Twilio Python: How to call Multiple calls by twilio - python

I Want To Call to Multiple phones by Twilio on python
I Just saw Twilio blog but nothing understood to me
this is my script
# Download the helper library from https://www.twilio.com/docs/python/install
import os
from twilio.rest import Client
account_sid = "AC723c6c38e7c58a572ce011a652540a42"
auth_token = "REDACTED"
client = Client(account_sid, auth_token)
# Your Account Sid and Auth Token from twilio.com/console
# and set the environment variables. See http://twil.io/secure
call = client.calls.create(
)
print(call.sid)
So Can You Tell Me What To Do

Twilio developer evangelist here.
You should make an array of phone numbers to call and then loop through that array, calling calls.create method to make a call to each number in the array passed in as a parameter to calls.create.
# Download the helper library from https://www.twilio.com/docs/python/install
import os
from twilio.rest import Client
account_sid = "AC723c6c38e7c58a572ce011a652540a42"
auth_token = "REDACTED"
client = Client(account_sid, auth_token)
# Your Account Sid and Auth Token from twilio.com/console
# and set the environment variables. See http://twil.io/secure
arr = ['INSERT-NUMBER-TO-CALL', 'INSERT-OTHER-NUMBER-TO-CALL'...]
for num in arr:
call = client.api.account.calls.create(
to=num,
from_='YOUR-TWILIO-NUMBER',
url = 'http://demo.twilio.com/docs/classic.mp3'
)
print(call.sid)
Alternatively, instead of url which would contain a link to a file you want to play on the phone, you could use twiml='<Response><Say>INSERT MESSAGE HERE</Say></Response>', to have a message played as well. You could use different Amazon Polly voices to customize the voice that speaks the message--more on that here.

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!

Fetch all active fone numbers for a twilio account with python library

i am trying to get all active phone numbers for an account using twilio python library. i tried the proposed in this solution but is outdated. Thanks.
To get all your active phone numbers in your account, you can request the incoming phone numbers API and iterate through the results which will automatically page through the API. To make fewer requests, you can change the page size up to 1000.
import os
from twilio.rest import Client
account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)
incoming_phone_numbers = client.incoming_phone_numbers.list(page_size=1000)
for phone_number in incoming_phone_numbers:
print(phone_number.sid)

can i automate this twilio verified phone numbers list from a python program

from twilio.rest import Client
import os
account_sid = 'ACXXXXXXXXXXXXXX'
auth_token = 'XXXXXXXXXXXXXXXXX'
client = Client(account_sid, auth_token)
mob = 'XXXXXXXXXX'
verify = client.verify.services('VAXXXXXXXXXXXXXXXXX')
verify.verifications.create(to=mob, channel='sms')
n = int(input('enter code: '))
result = verify.verification_checks.create(to=mob, code=n)
print(result.status)
after verifying the phone number i want to add that phone number to my twilio verified phone numbers lisI. I want to know if it is possible or not..
Yes you can do this by using the Twilio Voice API:
import os
from twilio.rest import Client
client = Client(account_sid, auth_token)
validation_request = client.validation_requests \
.create(
friendly_name='A friendly name',
phone_number='+1234567890'
)
print(validation_request.validation_code)
Taken and adapted from the Twilio documentation here.
Note:
This will create a new CallerID validation request within Twilio, which initiates a call to the phone number provided and listens for a validation code.
And:
Adding an Outgoing Caller ID via the API has the same result as verifying a number via the Twilio console.

Get Access Token for client profile Azure in Python

I'm looking for a way to get access token from a client profile when working with Azure using Python.
from azure.common.client_factory import get_client_from_cli_profile
from azure.mgmt.compute import ComputeManagementClient
client = get_client_from_cli_profile(ComputeManagementClient)
From the code I get the client profile context but how can I get access token from it?
I could find the method to get the access token from a client profile, to get the access token, you could use the adal, use which method depends on your requirement.
For example, I get the access token of a service principal with the client credentials to access the Azure Management REST API, the given resource is https://management.azure.com/.
import adal
# Tenant ID for your Azure Subscription
TENANT_ID = 'xxxxxxx'
# Your Service Principal App ID
CLIENT = 'xxxxxxx'
# Your Service Principal Password
KEY = 'xxxxxxx'
subscription_id = 'xxxxxxx'
authority_url = 'https://login.microsoftonline.com/'+TENANT_ID
context = adal.AuthenticationContext(authority_url)
token = context.acquire_token_with_client_credentials(
resource='https://management.azure.com/',
client_id=CLIENT,
client_secret=KEY
)
print(token["accessToken"])

Twilio SMS Python - Simple send a message

New to Twilio, have followed the SMS Python Quickstart guide successfully, and have combined two bits together, but now have some redundant code that I can't seem to get rid of without getting errors.
I have Python code that takes in coordinates from a text message, converts this into a Google Maps link, and sends that link to a different phone number.
However, currently it is also sending this reply to the original sender phone number, as that is what the original guide has you set up.
I only want it to send the message to the specified number, not reply to the original sender.
run.py:
# /usr/bin/env python
# Download the twilio-python library from twilio.com/docs/libraries/python
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
# 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 = 'account_sid'
auth_token = 'auth_token'
client = Client(account_sid, auth_token)
app = Flask(__name__)
#app.route("/", methods=['GET', 'POST'])
def sms_reply():
messages = client.messages.list()
print(messages[0].body)
coord = messages[0].body
lat,lon = coord.split(":")
mapURL = "https://www.google.com/maps/search/?api=1&query=" + lat + "," + lon
message = client.messages.create(
body=mapURL,
from_='+442030954643',
to='+447445678954'
)
"""Respond to incoming messages with a friendly SMS."""
# Start our response
resp = MessagingResponse()
# Add a message
resp.message(mapURL)
return str(resp)
if __name__ == "__main__":
app.run(debug=True)
Whenever I take out the lines I think are to do with the replying message to sender, it seems to break some of the other lines that I still need.
Any help much appreciated, thanks!
Twilio developer evangelist here.
You don't need to reply back to the incoming message and you can avoid this by returning an empty TwiML response.
As an extra win here, you don't have to call the API to get the body of the last message that was sent. That will be available in the POST request parameters to the endpoint. You can access those via request.form so the Body parameter will be available at request.form['Body'].
Try something like this:
# /usr/bin/env python
# Download the twilio-python library from twilio.com/docs/libraries/python
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
# 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 = 'account_sid'
auth_token = 'auth_token'
client = Client(account_sid, auth_token)
app = Flask(__name__)
#app.route("/", methods=['GET', 'POST'])
def sms_reply():
coord = request.form['Body']
lat,lon = coord.split(":")
mapURL = "https://www.google.com/maps/search/?api=1&query=" + lat + "," + lon
message = client.messages.create(
body=mapURL,
from_='+442030954643',
to='+447445678954'
)
# Start our empty response
resp = MessagingResponse()
return str(resp)
if __name__ == "__main__":
app.run(debug=True)
It's because your output in your sms_reply function is returning a TwiML that sends a message. I think it's normal to have some feedback from the service. If you don't want to reply with the mapURL you can just say somethink like "Thank you".
Otherwise, you can take a look to TwiML documentation to see what other actions you can do.

Categories

Resources