首页 > 解决方案 > Twilio 上的失败消息

问题描述

我使用flask和Twilio构建了一个whatapp聊天机器人,但是当我检查我的控制台时似乎收到了失败的消息。状态显示为失败,出现未知错误。当我检查我的调试器时,我看到了错误消息

HTTP retrieval failure

在对我的烧瓶应用程序进行一些更改后,我开始收到这些错误。我尝试撤消所有更改,但现在不是作为状态接收,而是状态现在显示为失败。我尝试重新启动我的 ngrok 并更改我的 Twilio 上的 url,但这也没有改变任何东西。

这是我的代码:

from flask import Flask, request
import requests
from twilio.twiml.messaging_response import MessagingResponse
import random
app = Flask(__name__)

@app.route('/bot', methods=['POST'])
def bot():
    incoming_msg = request.values.get('Body', '').lower()
    resp = MessagingResponse()
    msg = resp.message()
    responded = False
    if incoming_msg == 'help':
        #return options available
        output = 'This is a chatbot designed to send statistics, linear algebra and general programming problem questions. Please type in either "Python", "Statistics" or Linear Algebra" to get a random problem.'
        msg.body(output)
        responded = True
    if 'python' in incoming_msg:
        with open("C://Users//User//Downloads//python_test.txt", "r") as f:
            lines = f.readlines()
            code = random.choice(lines)
            msg.body(code)
            responded = True
    if 'quote' in incoming_msg:
        # return a quote
        r = requests.get('https://api.quotable.io/random')
        if r.status_code == 200:
            data = r.json()
            quote = f'{data["content"]} ({data["author"]})'
        else:
            quote = 'I could not retrieve a quote at this time, sorry.'
        msg.body(quote)
        responded = True
    if 'thegradientboost' in incoming_msg:
        message = 'Test \
                  '. Additional text' \
                  'More text,' \
                  'end of text.' \
                  'a bit more text' \
                  'conclusion.'
        msg.body(message)
        responded = True
    if not responded:
        msg.body('Word not included.')
    return str(resp)


if __name__ == '__main__':
    app.run(debug=True)

标签: pythonflasktwilio

解决方案


推荐阅读