SMS with Twilio Test Credential does not work - python

I am using Twilio Test crredentials o send a sample SMS message to my phone number which is already verified. When I run this script, it returns an SID and seems to work as expected only that I never receive the ext.. What am I missing?
from twilio.rest import Client
# Your Account Sid and Auth Token from twilio.com/user/account
account_sid = "AC3196c6a6fchha5de2c64a57e453e8e4b35"
auth_token = "fa1b7f6fdf3384ece5f5eda67cf7b42e28b"
client = Client(account_sid, auth_token)
message = client.messages.create(
to = "+12068515737",
from_ = "+15005550006",
body = "Jenny please?! I love you <3")
print(message.sid)

Twilio developer evangelist here.
That is the expected result when using test credentials. They are supposed to behave like real credentials, but never actually cause a message to be sent, or the call to be made or number to be bought. They are just for testing that the HTTP request was made correctly.
To send a message, you should use your real credentials. If you still have a trial account and a verified number, then you can send a message to that verified number using your trial account and it won't cost you anything. Only once you upgrade your account and add credit yourself will it cost to send an SMS message.
Let me know if that helps at all.

Related

Twilio update webhook for number by python code

Is there any way to set webhook url for Twilio number by python code ??
something as: client.number('MY_TWILIO_NUMBER').update(webhook='ngrok.io')
???
I want make automate update when i start new ngrok instance
Twilio developer evangelist here. You can accomplish this via the Twilio CLI as well as with Python using Twilio Incoming Phone Numbers! That Python code would look something like this (replacing the variables with your own account SID, auth token, and the phone number SID you'd like to use which can be found in the Console or via the CLI):
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)
incoming_phone_number = client \
.incoming_phone_numbers('PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
.update(voice_url='https://www.your-new-voice-url.com/example')
print(incoming_phone_number.friendly_name)
Let me know if this helps at all!

Verify message was sent with Twilio - Python

I have a small script that sends text messages with Twilio using Python.
This is my code:
import os
from twilio.rest import Client
account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
auth_token = os.environ.get('TWILIO_AUTH_TOKEN')
client = Client(account_sid, auth_token)
cell_number = os.environ.get('CELL_PHONE_NUMBER')
text_message = input("Enter a message to send: ")
send_message = client.messages.create(from_=os.environ.get('TWILIO_PHONE_NUMBER'),
to=cell_number,
body=text_message
print(send_message)
And this is the response I get back:
<Twilio.Api.V2010.MessageInstance account_sid=MY_ACCOUNT_SID sid=SMc5e8f335198144b4b3c7f401af511f11>
I was wondering what the best way was to validate that the message was actually sent in code.
I thought of doing something like this:
if send_message:
print("Message sent.")
else:
print("Message not sent.")
And I was just wondering if there was a better way to do that.
I understand that this is a relatively old question and you might have figured out a way to achieve the desired behavior, but just wanted to post my thoughts so that it may help other learners like me in the future.
So basically, twilio provides developers with a functionality of webhooks using which the status of messages can be tracked. You will have to provide an extra parameter while creating the message wherein you will have to pass the callback url pointing to a server which will be logging the status of the sent messages.
When your python script sends a SMS by doing a POST request to twilio server, twilio will send the status of the message to the callback URL as a parameter by making a post or a get request.
This document describes how to achieve the above behavior with the help of example code snippets. In case you used another method to track the status of the messages, let us know so that it will help the community.

Twilio Autopilot Python quickstart tutorial

I am trying to follow the following Twilio Autopilot tutorial https://www.twilio.com/docs/autopilot/quickstart/python?code-sample=code-create-your-first-assistant-with-autopilot-7&code-language=Python&code-sdk-version=6.x
I am trying to create my first assistant with autopilot and my own account_sid and auth_token.
# Download the helper library from
https://www.twilio.com/docs/python/install
from twilio.rest import Client
# Your Account Sid and Auth Token from twilio.com/console
# DANGER! This is insecure. See http://twil.io/secure
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'
client = Client(account_sid, auth_token)
assistant = client.autopilot \
.assistants \
.create(
friendly_name='Quickstart Assistant',
unique_name='quickstart-assistant'
)
print(assistant.sid)
However i receive the following error when i try execute the script.
Twilio developer evangelist here.
Did you by chance run this script more than once? The unique_name must be unique, so if you ran the script twice without changing that value, the second try would fail.
I tried running the code example you posted (with my own account credentials), and it worked the first time but failed the second time with the same exact error message you got (35002). Changing the unique_name value to quickstart-assistant-2, the script ran successfully again.
If you can log in to Twilio, you should be able to see the list of autopilot assistants you have created, which would tell you if you were able to create the assistant successfully on the first try.
Let me know if you have any additional questions or issues.

Twilio trigger Python function on SMS receive

Utilizing Twilio, upon receiving any SMS I'm seeking to trigger a Python function that reads the contents of the message, then conditionally performs an action.
I'm referencing from Twilio docs right now (.py):
# Download the helper library from https://www.twilio.com/docs/python/install
from twilio.rest import Client
# Your Account Sid and Auth Token from twilio.com/console
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'
client = Client(account_sid, auth_token)
message = client.messages('MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX').fetch()
print(message.to)
Does anyone know how to automatically set up a trigger OnReceive?
The example you show fetches a known message (a message that has already been received and you know its ID).
In order to have a script that is triggered by incoming message you need to setup a webhook and you need to have an endpoint (server) where you can pick up a trigger sent to you by twilio (eg at www.yourdomain.com/sms).
You might not be able to do it just from within a raspbery pi connected to the internet. You need to setup a trigger somewhere on a server, store the incoming messages somehow and then one solution would be to keep polling the server from raspberry pi, fetching new messages and clearing the list.

Can't get status of sms from Twilio

I had some issue when works with Twilio API. I tried to get sms status as is delivered, queue, failed or another, but I couldn't find this method at Rest API.
I use Python and Django.
def send_sms(self, msg):
ACCOUNT_SID = self.profile.twilio_sid
AUTH_TOKEN = self.profile.twilio_auth_token
client_phone_number = self.profile.phone_number
twilio_phone_number = "+1"+str(self.profile.twilio_number)
client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
message = client.messages.create(
body=msg,
to=client_phone_number,
from_=twilio_phone_number,
)
sid = message.sid
So how can I get sms status with sid what i have? Without response processing if it is possible
I found answer. All what I need is added to my code
body = client.messages.get(sid)
status =body.status
Twilio evangelist here.
To get an individual instance resource, use resources.ListResource.get(). Provide the sid of the resource you’d like to get.
msg = client.messages.get(sid)
print msg.to
You could also provide a status callback URL when you sent the message to have Twilio notify you via a webhook request when the message status changes:
https://www.twilio.com/docs/api/rest/sending-messages#post-parameters-optional
Hope that helps.
I found answer. All what I need is added to my code
body = client.messages.get(sid)
status = body.status
Message message = twilio.GetMessage(messageSid);

Categories

Resources