I'm trying to schedule an sms via Twilio. I'm referencing the demo found here: https://www.twilio.com/docs/sms/api/message-resource#schedule-a-message-resource
Per the demo, I set up messaging service via the Twilio Console.
Here is the code, with a dummy destination phone number and authorizations:
from datetime import datetime
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 = [sid]
auth_token = [token]
client = Client(account_sid, auth_token)
message = client.messages \
.create(
messaging_service_sid= [msid],
body='This is a scheduled message',
send_at=datetime(2022, 6, 18, 20, 36, 27),
schedule_type='fixed',
# status_callback='https://webhook.site/xxxxx',
to='+15559698489'
)
print(message.sid)
I get the following error:
TypeError: create() got an unexpected keyword argument 'send_at'
What am I missing?
Check that your Twilio Helper Library version is current.
Related
I am having trouble using Twilio's MessagingResponse class to send and receive messages using a webhook. I am using flask and ngrok to create a temporary URL to test my app, but I am getting a 502 error and 11200 warning with this new implementation.
I can confirm that the number I am attempting to message back is verified in my Twilio account. Here is the new implementation that uses the Twilio MessagingResponse to create the response message instead of sending it directly using the Twilio REST API:
import os
from flask import Flask, request, session, make_response
from twilio.twiml.messaging_response import MessagingResponse
from twilio.rest import Client
from chatbot import ask, append_interaction_to_chat_log
from secret_key import secret_key
app = Flask(__name__)
app.config['SECRET_KEY'] = secret_key
account_sid = os.environ.get('ACCOUNT_SID')
auth_token = os.environ.get('AUTH_TOKEN')
client = Client(account_sid, auth_token)
#app.route('/bot', methods=['POST'])
def bot():
incoming_msg = request.values['Body']
print(incoming_msg)
chat_log = session.get('chat_log')
answer = ask(incoming_msg, chat_log)
session['chat_log'] = append_interaction_to_chat_log(incoming_msg, answer, chat_log)
r = MessagingResponse()
r.message = answer
return make_response(r)
I have been able to successfully send and receive messages using a message object and explicitly stating the phone number I am sending to using this implementation:
import os
from flask import Flask, request, session
from twilio.twiml.messaging_response import MessagingResponse
from twilio.rest import Client
from chatbot import ask, append_interaction_to_chat_log
from secret_key import secret_key
app = Flask(__name__)
app.config['SECRET_KEY'] = secret_key
account_sid = os.environ.get('ACCOUNT_SID')
auth_token = os.environ.get('AUTH_TOKEN')
client = Client(account_sid, auth_token)
#app.route('/', methods=['POST'])
def bot():
incoming_msg = request.values['Body']
print(incoming_msg)
chat_log = session.get('chat_log')
answer = ask(incoming_msg, chat_log)
session['chat_log'] = append_interaction_to_chat_log(incoming_msg, answer, chat_log)
# use the incoming message to generate the response here
message = client.messages.create(
body=answer,
from_='+12232107883', #Twilio number you purchased or verified
to='+19143182181' # The phone number you want to send the message to
)
print(message.sid)
return 'message sent'
Attached is a photo of my implementation of the ngrok URL to configure the webhook.
Essentially, I am trying to implement the new structures in hopes of creating a more secure and scalable bot. Any ideas here? I realize it could be something in my Twilio settings, but I haven't found the solution. Thank you all.
I thought my problem was from not having an ngrok account:
But that did not seem to resolve the issue:
I have this python program that sends me daily emails. This is my personal email account with Microsoft outlook.com. My code has been working fine but broke yesterday. Here is my code
def email(subject, text):
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
user = "xxx#hotmail.com"
passwd = "xxxxxx"
sender = 'xxx#hotmail.com'
receiver = 'xxx#hotmail.com'
msg = MIMEMultipart('mixed')
msg['Subject'] = subject
msg['From'] = 'xxx#hotmail.com'
msg['To'] = 'xxx#hotmail.com'
text_plain = MIMEText(text,'plain','utf-8')
msg.attach(text_plain)
server = smtplib.SMTP('smtp.office365.com', 587)
server.ehlo()
server.starttls()
server.login(user, passwd)
server.sendmail(sender, receiver, msg.as_string())
server.quit()
User, sender, receiver, to and from are all the same email address. When I run the script, I got this error
>>> email('test subject', 'test message')
File "<stdin>", line 1, in <module>
File "<stdin>", line 19, in email
File "/usr/lib/python3.6/smtplib.py", line 730, in login
raise last_exception
File "/usr/lib/python3.6/smtplib.py", line 721, in login
initial_response_ok=initial_response_ok)
File "/usr/lib/python3.6/smtplib.py", line 642, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.3 Authentication unsuccessful [MW4PR03CA0229.namprd03.prod.outlook.com]')
Any ideas what could go wrong? This script has been working for at least half year..
Thanks!
Difan
not sure if I'll be of any help but since yesterday we are having problems with thunderbird connecting to microsoft mail server. For the base account changing authentication method to OAuth2 helped, but I still don't know what to do about aliases.
So I guess the problem lies with microsoft changing the requierements for authentication.
I can successfully send a message to my whatsapp using both the curl (bash) and python methods using the code here: https://www.twilio.com/console/sms/whatsapp/learn
However if I change the standard template body URL from yummycupcakes.com to a private IP (10.0.0.x), the message fails. Logs show:
Error: 63030 Unsupported parameter for type of channels message
Description: Unsupported parameter for type of channels message
Is it really trying to resolve links in my message body, and refusing to send the messageif it is un-resolvable? Very restrictive if so. Not clear if this is a twilio limitation or whatsapp...
Anyone with ideas here?
Example code:
from twilio.rest import Client
account_sid = '<my_sid>'
auth_token = '<my_authtoken>'
client = Client(account_sid, auth_token)
message = client.messages.create(
from_='whatsapp:+<my#>',
body='Your Yummy Cupcakes Company order of 1 dozen frosted cupcakes has shipped and should be delivered on July 10, 2019. Details: http://www.yummycupcakes.com/',
to='whatsapp:+<my#>'
)
print(message.sid)
I ran this code and did not get the error you encountered. It just does not render it as a clickable URL. Let me know if our code differs on something.
client = Client(account_sid, auth_token)
message = client.messages.create(
from_='whatsapp:+number',
body='Your Yummy Cupcakes Company order of 1 dozen frosted cupcakes has shipped and should be delivered on July 10, 2019. Details: http://{}'.format("10.0.0.22"),
to='whatsapp:+number'
)
print(message.sid)
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')
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!