Is it possible to send a message to 2 numbers (or more) with Twilio?
I wrote a script to scrape some stock prices and send it to me by Twilio.
My question can I make it to send the same message for me and other people?
.
Thanks in advance.
Twilio developer evangelist here.
You can send the same message to different people at different phone numbers! You would use Python code like this, replacing your account credentials with your own, the message to send, and the different phone numbers to send to.
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 = 'YOUR-ACCOUNT-SID'
auth_token = 'YOUR-AUTH-TOKEN'
client = Client(account_sid, auth_token)
arr = ['PHONE-NUMBER-TO-SEND-TO', 'PHONE-NUMBER-TO-SEND-TO'] #you can add more to this array
for num in arr: #loop through phone numbers in array
message = client.messages.create(
body='Message to send to different people',
from_='YOUR-TWILIO-PHONE-NUMBER',
to=num
)
print(message.sid)
Let me know if this helps at all!
Related
I am trying to send a text message that contains both text and a hypeprlink but am encountering the following message from the Twilio API:
"Error - 12300 Invalid Content-Type: Attempt to retrieve MediaUrl returned an unsupported Content-Type."
Here is the code I am attempting to leverage:
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)
message = client.messages \
.create(
body='Article: https://4r7s.short.gy/GPaoh7',
from_='123-345-5667',
to='123-345-5668',
)
When I send a message without a hyperlink it works fine (e.g. body = 'Here is the article for you to read') but when it contains a link I receive the aforementioned error. I've also tried using a shortened url of the above but that causes the same issue.
I was just able to send messages containing that exact link using my own Twilio account.
There might be an issue in that you are using phone numbers in local format, when they should be provided in e.164 format.
It's possible that your message is being blocked. Certain carriers don't like when you use link shorteners to obscure a link.
The error you are getting definitely seems weird, since you are not sending media. If you continue to have issues with this, I would contact Twilio support.
For clarification, I don't want to reply to the SMS. Every tutorial or document I've looked at is about setting up a port to listen on.
What I'm trying to do is just get the SMS and print it. I can send them fine and without problems.
Here is my sending function, and it works.
def send():
message = client.messages \
.create(
body=sendMSG,
from_='MY_TWILIO_NUMBER',
to='MY_PERSONAL_NUMBER'
)
print(message.sid)
How would you receive an SMS without Flask? Is there a way to do something similar to this method below just for receiving?
def receive():
message = client.messages \
.recieve(
from_='MY_PERSONAL_NUMBER',
to='MY_TWILIO_NUMBER'
)
print(message.sid)
I have not personally tried to get SMS messages from the logs before, always getting it directly through a webhook, but from what I see, it appears the command you might be looking for is list(). You can add filters, as shown in the API docs, and there are three filtering options. You can filter by DateSent, To, or From.
I have not tried this, but it would seem that the way to use this would be the following (adjusted from the code they supply):
# 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)
messages = client.messages.list(from='MY_PERSONAL_NUMBER', to='MY_TWILIO_NUMBER')
for record in messages:
print(record.sid)
If that doesn't work, the variables they use are actually capitalized "To" and "From", so you might try that.
After looking at that a bit, you might be looking more for this:
received = client.messages.list(to='MY_TWILIO_NUMBER')
sent = client.messages.list(from='MY_PERSONAL_NUMBER')
That will separate out those sent to you, and those sent from you
I want send message with telethon but i dont have phone number this .
i have only #username Telegram.
with this code i can send message for my contact phone :
result = client.invoke(ImportContactsRequest([contact], replace=True))
contacts = client.invoke(GetContactsRequest(""))
for u in result.users:
client.send_message(u, 'Hi')
But i want send message to #username Telegram
You can just do the following now:
client.send_message('username', 'hello')
Old answer:
It's on the Project's wiki, quoted below.
Via ResolveUsernameRequest
An "entity" is used to refer to either an User or a Chat (which includes a Channel). Perhaps the most straightforward way to get these is by resolving their username:
from telethon.tl.functions.contacts import ResolveUsernameRequest
result = client.invoke(ResolveUsernameRequest('username'))
found_chats = result.chats
found_users = result.users
# result.peer may be a PeerUser, PeerChat or PeerChannel
See Peer for more information about this result.
I would want to create an sms gateway to be sending sms to my site users with python, without using any sms gateway. how do i go about it.
I want it to be hosted in my site.
Not entirely sure what you mean by SMS gateway but there is a Python module that allows you to send SMS messages .
https://pypi.python.org/pypi/Clockwork/1.2.0
Basic example:
from clockwork import clockwork
api = clockwork.API('API_KEY_GOES_HERE')
message = clockwork.SMS(to = '441234123456', message = 'This is a test message.')
response = api.send(message)
if response.success:
print (response.id)
else:
print (response.error_code)
print (response.error_message)
Sorry if this isn't what you are looking for.
I am sending emails via sendgrid and would like to track the status of an email.
client = sendgrid.SendGridClient(username_or_apikey=my_key)
msg = sendgrid.Mail()
msg.add_to('a#foo.com')
msg.set_html('<div> hello there </div>')
msg.set_from('b#foo.com')
msg.set_subject('test sendgrid subject')
resp = client.send(msg)
The response object I am getting back is simply (200, '{"message":"success"}'). I was hoping to get back some for of email id.
I know sendgrid has webhooks, but how do I associate a sg_message_id with an email that I sent?
You can generate unique arguments on a per mail basis that go into the SMTP API JSON string and are included in status messages to webhooks.
More info at this page: https://sendgrid.com/docs/API_Reference/SMTP_API/unique_arguments.html