Call more than 10 numbers at once - twilio

We need to implement simultaneous GSM calls, but as stated in documentation (https://www.twilio.com/docs/api/twiml/number), "You can specify up to ten numbers within a verb to dial simultaneously" which looks like a very tough limit.
Could anyone please suggest a workaround for this?

You can do this via the REST API: https://www.twilio.com/docs/api/rest/making-calls.
A quick example would look something like this:
# Download the Python helper library from twilio.com/docs/python/install
from twilio.rest import TwilioRestClient
# Your Account Sid and Auth Token from twilio.com/user/account
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
auth_token = "your_auth_token"
client = TwilioRestClient(account_sid, auth_token)
# The following grabs a list of numbers that have sent a message to my Twilio number.
call_list = client.messages.list(to='+1415XXXXXXX')
# Then loop through all the numbers in the list and send a call from Twilio number via REST API.
for c in call_list:
client.calls.create(to=c.from_, from_="+1415XXXXXXX", url='YOUR_VOICE_URL')

Related

How to get a user's new Tweets with a Telegram Python bot while run_polling?

I'm currently developing a Telegram bot using telegram-python-bot and tweepy.
I want to create a feature that allows users of the bot to add their Twitter ID list via Telegram and have their new Tweets sent to them in real-time.
I want that the bot should be application.run_polling() to receive commands from the user, and at the same time, forwarding new tweets from Twitter users in users individual list.
When I read the tweepy documentation, I realized that I can get real-time tweets with fewer api requests if I fetch them through MyStream(auth=auth, listener=None).
But I don't know how to get both functions to work on the same file at the same time.
version
nest_asyncio-1.5.6 python_telegram_bot-20.0 tweepy-4.12.1
def main() -> None:
application = Application.builder().token("...").build()
add_list = ConversationHandler(
entry_points=[CallbackQueryHandler(input_id, pattern='input_id')],
states={ADD :[MessageHandler(filters.TEXT & ~filters.COMMAND, add)],},
fallbacks=[CallbackQueryHandler(button,pattern='back')])
application.add_handler(CommandHandler("on", on))
application.add_handler(add_list)
application.add_handler(CommandHandler("start", start))
application.add_handler(CommandHandler("list", list_setting))
application.add_handler(CommandHandler("admin", admin))
application.add_handler(CommandHandler("help", help_command))
application.add_handler(CallbackQueryHandler(button))
application.run_polling()
if __name__ == "__main__":
main()
This is my main statement and I made it work until the SIGINT(ctrl+c) came in via application.run_polling().
I want to combine the above code to run and do the following at the same time.
import tweepy
consumer_key = "..." # Twitter API Key
consumer_secret = "..." # Twitter API Secret Key
access_token = "..." # Twitter Access Key
access_token_secret = "..." # Twitter Access Secret Key
usernames = ['...']
auth = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret, access_token, access_token_secret
)
# Convert screen names to user IDs
user_ids = []
for username in usernames:
user = tweepy.API(auth).get_user(screen_name=username)
user_ids.append(str(user.id))
# Create a custom stream class
class MyStream(tweepy.Stream):
def __init__(self, auth, listener=None):
super().__init__(consumer_key, consumer_secret, access_token, access_token_secret)
def on_status(self, status):
tweet_url = f"https://twitter.com/{status.user.screen_name}/status/{status.id_str}"
print(f"{status.user.screen_name} tweeted: {status.text}\n{tweet_url}")
# send message to telegram
# Create a stream object with the above class and authentication
myStream = MyStream(auth=auth, listener=None)
# Start streaming for the selected users
myStream.filter(follow=user_ids)
I also tried to use thread's interval function or python-telegram-bot's job_queue.run_repeating function,
but these seem problematic for forwarding messages in real time.
I'm desperately looking for someone to help me with this😢.

How to disconnect the stream (Tweepy)?

I'm trying to update the stream every 15 minutes to change its rules.
As far as I understand it is impossible to update the filter rules in real time. So I try to stop the stream and then start it again.
class MyStream(tweepy.StreamingClient):
def disconnect(self):
self.running=False
print('stop stream)
stream = MyStream(bearer_token=bearer_token, wait_on_rate_limit=True)
stream.disconnect()
But it doesn't work. Streaming continues to work.
Can you please tell me how to reallocate what I have in mind?
update
I try to add a rule to the stream, then wait 10 seconds and add another one. But it doesn't work. Can you please tell me what the problem is and how to fix it?
import telebot
import tweepy
import time
bot = telebot.TeleBot()
api_key =
api_key_secret =
bearer_token =
access_token =
access_token_secret =
client = tweepy.Client(bearer_token, api_key, api_key_secret, access_token, access_token_secret)
auth = tweepy.OAuth1UserHandler(api_key, api_key_secret, access_token, access_token_secret)
api = tweepy.API(auth)
class MyStream(tweepy.StreamingClient):
def on_connect(self):
print('Connected')
def on_response(self, response):
print(response)
stream = MyStream(bearer_token=bearer_token, wait_on_rate_limit=True)
rules = ['book', 'tree', 'word']
#create the stream
for rule in rules:
stream.add_rules(tweepy.StreamRule(rule))
print('Showing the rule')
print(stream.get_rules().data)
stream.filter(tweet_fields=["referenced_tweets"])
# this part of the code no longer works.
print('sleep 10 sec')
time.sleep(10)
# this part not working too
print('Final Streaming Rules:')
print(stream.get_rules().data)
In Twitter API v2 (Tweepy using the tweepy.client interface and StreamingClient object) the stream does not need to disconnect in order to update the rules, you do that by adding rules via StreamingClient.add_rules(). Docs:
StreamingClient.add_rules() can be used to add rules before using StreamingClient.filter() to connect to and run a filtered stream:
streaming_client.add_rules(tweepy.StreamRule("Tweepy"))
streaming_client.filter()
StreamingClient.get_rules() can be used to retrieve existing rules
and
StreamingClient.delete_rules() can be used to delete rules.

Twilio Conference Call

I was able to implement basic Voice Conferencing but I feel my implementation may be lacking.
client = Client('ACxxxxxxxx', '34xxxxxxxxx')
#app.route('/', methods=["GET", "POST"])
def home():
form = ConferenceList() #form made using flask-wtf
if form.validate_on_submit():
contact_1 = form.data['contact_1'] #callee1
contact_2 = form.data['contact_2'] #callee2
from_ = form.data['from_'] #caller
response = VoiceResponse()
with Dial() as dial:
if from_ == MODERATOR:
dial.conference(
'Conf',
start_conference_on_enter=True,
end_conference_on_exit=True
)
else:
dial.conference('Conf', start_conference_on_enter=False)
response.append(dial)
'''here I feel could be a bottleneck'''
add_user(contact_1, conference_name='Conf', label='laed#1')
add_user(contact_2, conference_name='Conf', label="consumer")
return Response(str(response), 'text/xml')
return render_template('hello.html', form=form)
def add_user(contact, conference_name, label):
participant = client.conferences(conference_name).\
participants.create(
label=label, #label for participant
beep='onEnter',
record=True,
from_='from_', #same as above
to=str(contact)
)
if __name__ == '__main__':
app.run(debug=True, port=8000)
Basically the submit button triggers the / endpoint and the conference starts.
I feel there could be an issue with this implementation as I plan on cleaning it up and pushing to production (salesperson can make a conference call to leads on the app). Is there something I could have done better?
The voice(one client-one client) utilizes the Twilio Voice SDK, it there a way I could tweak it for conferencing?
You only need to respond with TwiML to a webhook from Twilio. In this case it appears that you are responding with TwiML to your application's front end when a submit button is pressed.
So, you can drop all the TwiML:
#app.route('/', methods=["GET", "POST"])
def home():
form = ConferenceList() #form made using flask-wtf
if form.validate_on_submit():
contact_1 = form.data['contact_1'] #callee1
contact_2 = form.data['contact_2'] #callee2
'''here I feel could be a bottleneck'''
add_user(contact_1, conference_name='Conf', label='laed#1')
add_user(contact_2, conference_name='Conf', label="consumer")
return "whatever"
Since the settings you are trying to apply in the TwiML do not apply to the participants joining the conference, you need to adjust your add_user function to use them. In this case, the startConferenceOnEnter and endConferenceOnExit parameters for the moderator should be sent when you create the participant. It might be easier to write two methods, add_user and add_moderator, to make things clear:
def add_user(contact, conference_name, label):
participant = client.conferences(conference_name).\
participants.create(
label=label, #label for participant
beep='onEnter',
record=True,
from_='from_', #same as above
to=str(contact),
start_conference_on_enter=False
)
def add_moderator(contact, conference_name, label):
participant = client.conferences(conference_name).\
participants.create(
label=label,
beep='onEnter',
record=True,
from_='from_',
to=str(contact),
start_conference_on_enter=True,
end_conference_on_exit=True
)
Then call different functions for the different participants:
add_moderator(contact_1, conference_name='Conf', label='laed#1')
add_user(contact_2, conference_name='Conf', label="consumer")
When you call add_user or add_moderator it will make an API request and slow down your server response. If you wanted to offload those requests to a worker, that would make your response quicker. But for 2 API requests, it is likely not a problem.
One other thing you might want to consider is the consumer experience. If they answer the phone before your agent does, then they will be greeted with hold music. You might want to architect it so that the application calls the agent first and only once they have picked up it then dials the consumer. Just worth considering.
Edit
After further explanation, you are now telling me that you want to make the call from the browser using the Twilio Voice SDK for JS.
To make outbound calls with the JS SDK you need to create an access token which includes an outgoing application sid, which refers to a TwiML application. That TwiML application has a voice URL. When you place the call with the SDK, Twilio makes a webhook request to the voice URL of your TwiML app. Your application can perform actions and return TwiML to tell Twilio what to do with the call.
When you create the call with the JS SDK you can pass parameters to the call.
const device = new Device(token);
const call = await device.connect({
params: {
To: ["+15551234567", "+145557654321"]
}
});
Those parameters are sent with the webhook request to your TwiML App voice URL. You can then use a response very similar to your original code to respond here, because you need to return TwiML to the request from Twilio, and start calls to the other participants in the call.
#app.route('/conference', methods=["POST"])
def conference():
const numbers = request.form["To"]
response = VoiceResponse()
with Dial() as dial:
dial.conference(
'Conf',
start_conference_on_enter=True,
end_conference_on_exit=True
)
response.append(dial)
for number in numbers:
add_user(number, conference_name='Conf')
return Response(str(response), 'text/xml')
This code receives the To parameter, a list of numbers to dial into this conference, builds the TwiML response that will put the browser caller into a conference call, places outbound calls to the numbers to dial them into the conference and then returns the TwiML to Twilio. The dialler in the browser will start the conference and the other participants will arrive in the conference when they answer the phone.
In this case you don't make the request to your server yourself, you use the JS SDK to trigger the call and let Twilio make the request to your server. As mentioned in the comments, you may want to offload the API calls to create participants to a background job so that you can respond to the webhook request quicker, but that is beyond the scope of this answer.

Using Twilio I want to add a third party into a call using an iOS Device

I'm using Twilio in an iOS app using the TwilioClient library in Swift. The app I've built can connect from user A's device to user B's device or to an outside phone number. This is working fine. What I need to do now is to bring a third party into the call, either a third iOS device (user C) or an additional outside line.
I understand that I will need to use the Conference capability to do this, but I'm not finding examples of how to do this with the TwilioClient library for iOS. Currently my TwiML server is the Python quick start server (which has been fine for two-party calls).
What I'm unclear on is:
Using the TwilioClient device.connect(...) call how do I connect User A & User B into a conference room in the first place?
Once I have A & B in the conference call, how, using TwilioClient, do I bring in party C? I've connected with device.connect(...) which hides all of the REST API code from me.
Since I don't know at the beginning of the call if I'll need the third party is there a way to start off with a direct connect then add all parties to a conference call once we decide we need to add User C?
Thanks,
KeithB
Below is an answer on how to solve question 1 in my OP.
When you use the iOS call device.connect(...) it calls the TwiML server /call URL (in my case a Python server), which returns "TwiML" (Twilio Markup Language). The basic pattern is to call a destination directly. To do this the TwiML server uses the passed parameters To: and From: and constructs a TwiML "Dial" string that is returned. This causes Twilio to dial the To: number using the From: number as a callerid.
In order to have both parties put into a conference it is a two step process. First you modify your TwiML code to tell Twilio to put the Caller into the conference room. Second, you use the REST API at the TwiML server to have Twilio call out to the To: caller and put them in the conference room.
This requires several changes. First you must pass in the conference room name from the device.connect(...) call in your iOS app.
connection = device?.connect(["To": "+1234567890", "Conference": "MyConference"], delegate: delegate)
Then you have to modify the /call route in the Python server to return TwiML for a conference room AND make the outbound REST call.
from twilio.rest import TwilioRestClient
#app.route('/call', methods=['GET', 'POST'])
def call():
account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
auth_token = os.environ.get("AUTH_TOKEN", AUTH_TOKEN)
from_value = request.values.get('From')
to = request.values.get('To')
conf_room = request.values.get('Conference')
callsid_value = request.values.get('CallSid')
# This section creates the TwiML code that will be returned
# This causes the caller to be placed in a conference room
# Note: endConferenceOnExit=true ends conference when call originator exits
conf_response = twilio.twiml.Response()
conf_response.dial(callerId=from_value).conference(conf_room, endConferenceOnExit="true")
# This section makes the REST call out to the To: number
client = TwilioRestClient(account_sid, auth_token)
call = client.calls.create(url="www.YourPythonServer" + "/getconf?Conference=" + conf_room,
to=to,
from_=from_value,
method="POST",
status_callback="www.YourPythonServer" + "/callback?init_callsid=" + callsid_value,
status_callback_method="POST",
status_events=["completed"])
return str(conf_response)
When we call client.calls.create(...) it causes Twilio to then call the URL "www.yourpythonserver/getconf..." to get the TwiML it uses to know how to execute the outgoing call.
#app.route('/getconf', methods=['GET', 'POST'])
def getconf():
resp = twilio.twiml.Response()
conf_room = request.values.get('Conference')
resp.dial().conference(conf_room)
return str(resp)
You'll notice up above we also have a parameter "status_callback". This is used to let us know the To: caller's status. In this case we request 'complete' but we may also get "no-answer", "busy" & "failed". If we don't do this and the call is unsuccessful then the caller will just sit in an empty conference room w/o knowing the outgoing call was unsuccessful. This requires yet another route in our Python server.
#app.route('/callback', methods=['GET', 'POST'])
def callback():
account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
auth_token = os.environ.get("AUTH_TOKEN", AUTH_TOKEN)
init_callsid = request.values.get('init_callsid')
callback_status = request.values.get('CallStatus')
# Check to see what status was passed in. Note that "completed" is
# passed after a successful call and the To: party hangs up or drops.
# You can implement any behavior you want here, but this code
# simply hangs up the originating caller. This might not be appropriate
# if there are still other parties in the call.
if callback_status == 'completed' or callback_status == "no-answer" or callback_status == 'busy' or callback_status == 'failed':
client = TwilioRestClient(account_sid, auth_token)
call = client.calls.hangup(init_callsid)
return str("")
The following is an answer to question #2 in my OP.
After step #1 we have a conference room named 'MyConference' with parties 1 & 2 (From & To). Now we would like to add a third party to the conference and trigger that action from our iPhone.
What we need to do is create a new Route in our python server and call that route directly from our iOS code.
The code in the python server will look something like this:
#app.route('/add_to_conference', methods=['GET', 'POST'])
def add_to_conference():
account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
auth_token = os.environ.get("AUTH_TOKEN", AUTH_TOKEN)
from_value = request.values.get('From')
to = request.values.get('To')
conf_room = request.values.get('Conference')
client = TwilioRestClient(account_sid, auth_token)
call = client.calls.create(url="www.YourPythonServer" + "/getconf?Conference=" + conf_room,
to=to,
from_=from_value,
method="POST",
status_callback="www.YourPythonServer" + "/callback?Conference=" + conf_room,
status_callback_method="POST",
if_machine = "Hangup",
status_events=["completed"]
)
return str("True")
The code above will make a REST call that will cause Twilio to make an outgoing call to the "To" phone number and place the party in the conference room. You will notice that like the code in step #1 we employ the '/getconf' route. The Twilio server will call this URL to get the TwiML that defines how it should process the call.
#app.route('/getconf', methods=['GET', 'POST'])
def getconf():
resp = twilio.twiml.Response()
conf_room = request.values.get('Conference')
resp.dial().conference(conf_room)
return str(resp)
We also defined a '/callback' route that is called upon call completion (hangup) or failure of caller #2 or #3. Since answering Step 1 I have enhanced the way this callback works. My goal is that when all 'To' parties hang up, leaving only the original caller in the conference room, the call should hangup for the original caller. Otherwise, the From caller will just be left in the conference room listening to music. This is not what is expected.
Therefore I implemented the following callback code:
#app.route('/callback', methods=['GET', 'POST'])
def callback():
account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
auth_token = os.environ.get("AUTH_TOKEN", AUTH_TOKEN)
callback_status = request.values.get('CallStatus')
conf_room = request.values.get('Conference')
client = TwilioRestClient(account_sid, auth_token)
conferences = client.conferences.list(friendly_name=conf_room, status = "in-progress")
if (len(conferences) > 0):
conference = conferences[0]
participants = client.participants(conference.sid).list()
if len(participants) == 1:
participant = participants[0]
call = client.calls.hangup(participant.call_sid)
return str("200")
When called, this code grabs the conference room by name, then grabs the list of participants in the conference room. If there is only one participant remaining then it hangs up on the last participant.
The final step is making the call from your iOS code. This is quite simple, you do not need to use the Twilio client libraries for this call, you simply make an http GET request passing in From, To and the Conference room name as parameters. Like this:
http:/yourpythonserver.com/add_to_conference?To=1234567890&From=0987654321&Conference=MyConference
This will activate the entire process and party #3 will be placed in the conference room!

Avoiding repeated outbound calls from twilio python program

This is my python script that I run from command line.It makes a call to the 10_DIGIT_NO.If I dont answer the call it repeatedly makes calls.The twiml was generated via https://www.twilio.com/labs/twimlets/my/create?type=callme. How to avoid this??
I want the call to be made just once irrespective of it being received or not.
Thanks
# Download the library from twilio.com/docs/libraries
from twilio.rest import TwilioRestClient
# Get these credentials from http://twilio.com/user/account
account_sid = "xyz"
auth_token = "abc"
client = TwilioRestClient(account_sid, auth_token)
# Make the call
call = client.calls.create(to="10_DIGIT_NO", # Any phone number
from_="MY_TWILIO_NO", # Must be a valid Twilio number
url="XML_FILE_PATH")
print call.sid
Twilio Developer Evangelist here. It looks like we'll need to dig into your account a bit to diagnose this properly - I would recommend e-mailing help#twilio.com so that we can help you find a solution!

Resources