How do I get the conversation SID using data from Messaging Monitor screen?

754 Views Asked by At

I have an old Conversation's Channel ACTIVE with my phone number, so the incoming messages are not entering the Studio's flow. I need the Conversation's Channel SID so I can close that channel using the API Explorer.

The only data I have is that the message was received using the Monitor>Messaging screen in Twilio Console.

Monitor Image description

But I don't find an API that I can use to get the SID I need with the data I have

Right now, I built a bash script to fetch all conversations and (when the script finishes) I will grep the whatsapp number to find the conversation SID.

#!/bin/bash
ACCOUNT_SID=ACXXXXXXXXXXXXXXXXX
AUTH_TOKEN=XXXXXXXXXXXXX
PAGE_SIZE=100
DOWNLOAD_PAGES=1000
CURRENT_PAGE=${DOWNLOAD_PAGES}
DOWNLOAD_DIR=downloaded_channels
rm -rf ${DOWNLOAD_DIR}
mkdir -p ${DOWNLOAD_DIR}

while (( ${CURRENT_PAGE} >= 0 )); do
  echo "downloading Page ${CURRENT_PAGE}"
  curl 'https://conversations.twilio.com/v1/Conversations' -u ${ACCOUNT_SID}:${AUTH_TOKEN} > ${DOWNLOAD_DIR}/page_${CURRENT_PAGE}.json
  sleep 0.1
  (( CURRENT_PAGE=$CURRENT_PAGE-1 ))
done

The problem with this approach is that I have thousands (if not millons) of conversations and Twilio's API allow result pages of 100 elements max. So if I'm not lucky to find my conversation in the first downloaded pages, I will be here forever.

Does anyone know a better approach for this?

2

There are 2 best solutions below

0
On BEST ANSWER

If you're using the Conversations API, you can use the Participants Conversations API to search by your address on Twilio and get all conversation assigned to your number.

Follow the documentation of API: https://www.twilio.com/docs/conversations/api/participant-conversation-resource

Follow an example using the Twilio CLI:

twilio api:conversations:v1:participant-conversations:list --address "whatsapp:+myNumber" --properties conversationSid,conversationState --no-limit

If you're using the Programmable Chat, doesn't have an API to search by your phone, but you can search all and use a filter to get just the Channels with your number on attributes (The process can during a long time depending of the quantity of conversations in your account).

Follow an example using the Twilio CLI:

twilio api:chat:v2:services:channels:list --service-sid <service_sid> --properties sid,attributes --no-limit | grep myNumber

I hope that it can help you! :D

0
On

Twilio fanatic here.

According to the Conversation resource page, as you are using CURL you can specify the request parameters in the request URL, similar to how you would using Twilio's native API functions (read()).

From the Conversation Resource documentation (modified to include State and DateCreated parameters):

curl -X POST "https://conversations.twilio.com/v1/Conversations" \
--data-urlencode "State=active" \
--data-urlencode "DateCreated=>=YYYY-MM-DD" \
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

Please be sure to urlencode the DateCreated parameter as it includes non-URL friendly symbols (>= to reference on or after- I am not sure if the written format is suitable for bash as-is).

You may use any of the 10 available Conversation parameters to specify which Conversations are returned. As stated, one or more lines or variable names may not be suited for your application as-is, but with a bit of finagling should produce the result you're looking for. For example, DateModified or looking for a custom value placed under attributes might suit your case better.

If this helps, please upvote. I am trying to get noticed by Twilio and get hired to fulfill my dream of working while RVing fulltime.