Twilio - Quick question (Unable to update record) - twilio

hope you are doing it right these days.
To summarize my problem, I think this is not working becuase I am using a free Twilio account instead of a paid one. But that's just my beginner theory. Now, the issue:
I have tried an official Twilio tutorial (https://www.twilio.com/blog/automating-ngrok-python-twilio-applications-pyngrok, I shared the link in case someone finds it interesting or needs it), which allows us to automate SMS webhook (sms_url) configuration by using Client (twilio) and pyngrok (ngrok).
def start_ngrok():
from twilio.rest import Client
from pyngrok import ngrok
url = ngrok.connect(5000)
print(' * Tunnel URL:', url)
client = Client()
client.incoming_phone_numbers.list(
phone_number=os.environ.get('TWILIO_PHONE_NUMBER'))[0].update(
sms_url=url + '/bot')
I can't explain all the things that I tried in the last 4 days, with no success. I keep getting the same error:
client.incoming_phone_numbers.list(phone_number=os.environ.get('TWILIO_PHONE_NUMBER'))[0].update(sms_url=url + '/bot')
IndexError: list index out of range
Something is not working with the list, it comes empty, although environment variables are working properly. I will work with just one phone_number, so there no need for list, indeed, so I started to change that line to avoid different errors and ended up with this:
def start_ngrok():
from twilio.rest import Client
from pyngrok import ngrok
url = ngrok.connect(5000)
print(' * Tunnel URL:', url)
client = Client()
client.incoming_phone_numbers("my_number").update(sms_url=str(url) + '/bot')
Then I got the final error that I can't solve by my self:
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twilio/rest/api/v2010/account/incoming_phone_number/__init__.py", line 442, in update
payload = self._version.update(method='POST', uri=self._uri, data=data, )
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/twilio/base/version.py", line 106, in update
raise self.exception(method, uri, response, 'Unable to update record')
twilio.base.exceptions.TwilioRestException:
HTTP Error Your request was:
POST /Accounts/my_account_SID/IncomingPhoneNumbers/+my_number.json
Twilio returned the following information:
Unable to update record: The requested resource /2010-04-01/Accounts/my_account_SID/IncomingPhoneNumbers/+my_number.json was not found
More information may be available here:
https://www.twilio.com/docs/errors/20404
I tried all different phone numbers combinations/formats: nothing works.
Thanks for your time reading all this!

Looks like something changed since the blog was written or there was a mistake.
Try the below:
The only difference is adding .public_url to the url object. Also allowed a GET to /bot for testing.
from dotenv import load_dotenv
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
load_dotenv()
app = Flask(__name__)
#app.route('/bot', methods=['POST','GET'])
def bot():
user = request.values.get('From', '')
resp = MessagingResponse()
resp.message(f'Hello, {user}, thank you for your message!')
return str(resp)
def start_ngrok():
from twilio.rest import Client
from pyngrok import ngrok
url = ngrok.connect(5000)
print('This is',url)
print(' * Tunnel URL:', url)
client = Client()
client.incoming_phone_numbers.list(
phone_number=os.environ.get('TWILIO_PHONE_NUMBER'))[0].update(
sms_url=url.public_url + '/bot')
if __name__ == '__main__':
if os.environ.get('WERKZEUG_RUN_MAIN') != 'true':
start_ngrok()
app.run(debug=True)

Related

Trouble using MessagingResponse class in Twilio library to receive and reply to SMS

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:

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.

Get and Post data in Flask request

Hello and thanks in advance for any help.
I am trying to set up a ReactJS & Flask Web App, but i am having trouble getting the data into Flask, i am using Insomnia to test send the data to Flask, and the POST request returns code 200, but i keep on getting the error on the printscreen below, UnboundLocalError: local variable 'text' referenced before assignment. The string is not passed to the TTS (text-to-speech) class for processing, when i use direct assignment for the strings on the comented code bellow the imports works fine.
I have tried to send the data with JSON, now i am trying with form format, it returns the same error.
Can you help me, please, and take a look at my code ?
from flask import Flask, render_template, request
import speak
# text = "AI AI minha machadinha !!"
# lang = "pt"
app = Flask(__name__, static_folder="../static/dist", template_folder="../static")
#app.route("/", methods=["GET","POST"])
def index():
return render_template("index.html")
#app.route("/hello", methods=["GET","POST"])
def hello():
if request.method == "POST":
text = request.form["text"]
lang = request.form["lang"]
print("passou")
return speak.get_speak(text,lang)
if __name__ == "__main__":
app.run()
Insomnia code 200 message
error on console log
Try this below :
#app.route("/hello", methods=["GET","POST"])
def hello():
text = ''
if request.method == "POST":
text = request.form["text"]
lang = request.form["lang"]
print("passou")
return speak.get_speak(text,lang)
This is beacause your text is defined inside if condition and your return is outside the if condition. You need to define it above the if and give it a default value.

Why Am I Getting An Error When Connecting To Google Search Console API Via Flask Dance?

Im creating a flask app that retrieves data from Google Search Console API.
However I'm having hard times implementing Google OAuth with Flask-Dance.
I am getting the following error:
Here is my code:
from flask import Flask, render_template, request, redirect, url_for
from flask_dance.contrib.google import make_google_blueprint, google
import os
def create_app(config_name):
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1'
print(config_name)
blueprint = make_google_blueprint(
client_id={MY CLIENT ID},
client_secret={MY SECRET},
scope=["profile", "email"])
app = Flask(__name__)
app.config.from_object(config[config_name])
google_bp = make_google_blueprint(scope=["profile","email","https://www.googleapis.com/auth/webmasters"])
#app.route("/search")
def search():
if not google.authorized:
return redirect(url_for("google.login"))
request = {
'startDate': '2019-01-01',
'endDate': '2019-01-31','dimensions': ['query']}
resp = google.post("/webmasters/v3/sites/https%3A%2F%2Fwww.mysite.com/searchAnalytics/query", json=request)
amt=resp['rows'][0]['clicks']
return '<h1>'+amt+'</h1>'
return app
I have also set up the application in Google Developers Console following the below steps, outlined here:
https://flask-dance.readthedocs.io/en/v0.8.0/quickstarts/google.html
Any ideas what might be the issue?
Many thanks in advance
From the screenshot, client_id=None, it looks like you have some misconfiguration. In your code, there are two make_google_blueprint.
You can review your code again to fix this problem.

Can't make custom Twilio Call with TwiML Bin and Assets

I am trying to make a twilio call with hosting an xml file on Twilio TwiMl Bins and hosting an mp3 file in Assets:
I just want to say a simple sentence and then play a 5-10 second clip. Currently when I run my code:
# Download the helper library from https://www.twilio.com/docs/python/install
import os
from twilio.rest import Client
# Your Account Sid and Auth Token from twilio.com/console
account_sid = "stuff"
auth_token = "stuff"
client = Client(account_sid, auth_token)
call = client.calls.create(
url="https://handler.twilio.com/twiml/EHdff1ba57cc168191864794c6c44c1723",
from_="+1mytwilionumber",
to="+1mynumber"
)
print(call.sid)
I just get a call - the trial message and then silence and then the call is concluded. Any tips?
Never Mind! So this worked:
https://support.twilio.com/hc/en-us/articles/223132187--Not-Authorized-error-when-trying-to-view-TwiML-Bin-URL
But your twilio call will only run once after you "sign" the HTTP request with hmac authentication. The script is
const crypto = require('crypto')
, request = require('request')
const url = process.argv[2] + '?AccountSid=' + process.env.TWILIO_ACCOUNT_SID
const twilioSig = crypto.createHmac('sha1', process.env.TWILIO_AUTH_TOKEN).update(new Buffer(url, 'utf-8')).digest('Base64')
request({url: url, headers: { 'X-TWILIO-SIGNATURE': twilioSig }}, function(err, res, body) {
console.log(body)
})
Make sure to have node, npm, and request module installed.
npm install request --save
and you are good to go! Run this script right before your python calling script.
Just an fyi you can also make TWIML/robocalls on the fly by uploading your TWIML to a bin, just before you make the call. It just needs a public URL. This project that I found actually demonstrates the technique by uploading the TWIML/XML on the fly, getting the new URL (for the newly generated TWIML), and then makes the Twilio call. That restwords.com site is pretty handy for this purpose.

Resources