简体   繁体   中英

Get message from Twilio REST API throws 'SmsMessages object is not callable'

I am trying to get a message object from the Twilio REST API, and it's not working.

I have a class method that creates and sends a sms message with Twilio. It looks something like this:

def send_sms(self, recipient, body):
    client = twilio.rest.TwilioRestClient(
        self._account_sid,
        self._auth_token)

    try:
        message = client.sms.messages.create(
            to=recipient,
            from_=self._from,
            body=body)
    except twilio.TwilioRestException as error:
        # do some stuff...

    return message

I have another class method that is supposed to get the message object from Twilio, so I can check the status:

def get_sms(self, sid):

    client = twilio.rest.TwilioRestClient(
        self._account_sid,
        self._auth_token)

    message = client.sms.messages(sid).fetch()
    return message

Unfortunately, my attempt to get the message using the sid is not working. The message = client.sms.messages(sid).fetch() line method throws an error: TypeError: 'SmsMessages' object is not callable .

This is frustrating, because according to the docs I am doing this correctly. I have tried removing the .sms. from the line also!

I was mixing use of the TwilioRestClient , from the REST API v5.x, with use of fetch() , from the REST API v6.x. For future reference, there is a tiny version toggle button in the top right corner of the code explorer in the Twilio docs!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM