0

I am trying to fetch a conversation following Twilio documentation verbatim here.

import os
from twilio.rest import Client


# Find your Account SID and Auth Token at twilio.com/console
# and set the environment variables. See http://twil.io/secure
account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)

conversation = client.conversations \
                     .conversations('CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
                     .fetch()

This returns a :

HTTP 404 error: Unable to fetch record: The requested resource /Conversations/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX was not found

However if I supply the conversation service id like so:

import os
from twilio.rest import Client


# Find your Account SID and Auth Token at twilio.com/console
# and set the environment variables. See http://twil.io/secure
account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)

conversation = client.conversations \
                     .services("ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
                     .conversations('CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
                     .fetch()

I get the conversation object correctly. In my case I am using webhooks to catch when a participant joins a conversation, since the event doesn't return the service sid, only the conversation sid, it complicates what I am trying to do. What am I missing?

MweyaMutsvene
  • 543
  • 4
  • 15

1 Answers1

1

Twilio developer evangelist here.

The short form conversations API, like this:

conversation = client.conversations \
                     .conversations('CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
                     .fetch()

only works if you are using the default service. Otherwise you do need to provide the service SID.

If I were you I would store the conversations service SID as a config parameter in your application, perhaps set as an environment variable, so that you can refer to it when making requests to the API like this.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • Gotcha - I wish the webhook events would contain not just the conversation sid but the conversation service sid. My app is making using of dozens of different conversation services, and I was hoping I wouldn't have to create my own lookup table. – MweyaMutsvene Aug 09 '21 at 07:33
  • There is a case internally to add this parameter to the webhooks, so hopefully you can do away with the lookup table at some point. – philnash Aug 09 '21 at 08:43