如何在 python 中与 twilio 入站消息来自用户交互?

问题描述

大家好,我在与我创建的 Twilio StudioFlow 用户的消息交互时遇到问题。在整个执行过程中,我希望我的程序能够识别用户何时发送诸如“停止”或“开始”之类的消息,以便它根据用户的响应更新数据库

我的问题是我缺少哪些工具来与我的代码中的入站消息进行交互?我正在使用烧瓶来构建什么是短信聊天机器人,并且从 twilio 文档中我想出了这个:

# Imports of routes.py file 

from flask import request,redirect
from twilio.twiml.messaging_response import MessagingResponse
from smsint.models import Part,parts,phone_nums
from smsint import app,db,client,sid,token

@app.route('/sms',methods=['POST','GET']) # route for main chat service
def flow():

    week_1 = client.studio.flows('FWXXXXXXXXXXXXXXXXXXXX').executions.create(to='+XXXXXXXXXX',from_='+XXXXXXXXXX') # studioflow execution for first week

    # TwiML Resp
    resp = MessagingResponse()
    msg = resp.message

    # message user sent from twilio
    body = request.values.get('Body').lower()
    number = request.values.get('From')
    part = Part.query.filter_by(phone_num=number).first()

    # logic
    if body == 'stop':
        part.stop = True
        db.session.commit()

    elif body == 'start':
        part.stop = False
        db.session.commit()

    # ------- SAMPLE CODE ---------
    # Possible call to database phone numbers
    '''
    numbers = Part.query.with_entities(Part.phone).all()

    for i in phone_nums:
        execution = client.studio.v1.flows('FWXXXXXXXXXXXXXXXXXXXXX').executions.create(
            to=i,from_='+XXXXXXXXXXX')
        execution
    '''
    # ------- SAMPLE CODE ---------

如您所见,我正在使用来自flask 的请求,但我确定我没有正确使用它,因为从测试中我从nonetype 返回了一个body = request.values.get('Body').lower() 对象,{{1} 也是如此

总而言之,如何使用 Flask 请求或其他模块(如请求模块本身)与入站消息的属性(“发件人”编号,消息正文)交互到我的 Python 代码中的 StudioFlow 执行?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)