【问题标题】:nested if else in Twilio在 Twilio 中嵌套 if else
【发布时间】:2021-04-07 12:35:03
【问题描述】:

我正在使用 Twilio 和 python 制作一个自动化的 whatsapp 回复机器人,但是我遇到了问题并且无法在其中使用嵌套的 if else

from flask import Flask, request
import requests
from twilio.twiml.messaging_response import MessagingResponse

app = Flask(__name__)

@app.route('/mybot', methods = ['POST'])

def mybot():
    incoming_msg = request.values.get('Body', '').lower()
    resp = MessagingResponse()
    msg = resp.message()
    responded = False

    print(incoming_msg)
    if incoming_msg == "list of doctors":
        msg.body("We have Dr. Phil with us today")
        responded = True

    if "book appointment" in str.lower(incoming_msg) or "see a doctor" in str.lower(incoming_msg):
            msg.body("Which department would you like to visit?\nPress :-\n1 - General Surgery\n2 - Internal Medicine\n3 - Gynaecology\n4 - Obstetrics\n5 - Ophthalmology\n6 - Orthopaedics\n7 - Dermatology Venereology & Leprology\n8 - ENT\n9 - Paediatric")
            if (incoming_msg == "1" or "general surgery" in incoming_msg): # This statement gets ignored
                msg.body("General Surgery")
            responded = True

if (incoming_msg == "1" or "general surgery" in incoming_msg): 是问题陈述。

有没有办法解决这个问题?

【问题讨论】:

  • 是什么让您认为它被忽略了?你在它之前有print(incoming_msg)吗?输出是什么?
  • @Axe319 我在嵌套的 if 中使用了 print("HI") 语句,但它没有在控制台中打印。另外,这是控制台日志以防万一... 127.0.0.1 - - [07/Apr/2021 18:15:40] “POST /mybot HTTP/1.1” 200 - 看医生 127.0.0.1 - - [07/ 2021 年 4 月 18:15:41]“POST /mybot HTTP/1.1”200 - 127.0.0.1 - - [07/Apr/2021 18:15:42]“POST /mybot HTTP/1.1”200 - 127.0.0.1 - - [07/Apr/2021 18:15:43] “POST /mybot HTTP/1.1”200 - 127.0.0.1 - - [07/Apr/2021 18:15:45] “POST /mybot HTTP/1.1”200 - 1
  • 你能在if "book appointment" in str.lower(incoming_msg) or "see a doctor" in str.lower(incoming_msg):这一行之前和之后放置一个print(incoming_msg)并发布输出吗?
  • @Axe319 127.0.0.1 - - [07/Apr/2021 20:28:53] "POST /mybot HTTP/1.1" 200 - 看医生看医生 127.0.0.1 - - [07 /Apr/2021 20:28:56]“POST /mybot HTTP/1.1”200 - 127.0.0.1 - - [07/Apr/2021 20:29:01]“POST /mybot HTTP/1.1”200 - 普通外科
  • 你的问题。第一个incoming_msg'see a doctor',它不会通过你的内部if 语句,你的第二个是'general surgery',它不会通过外部if

标签: python-3.x twilio whatsapp twilio-api


【解决方案1】:

在这种情况下,您不能嵌套它们。用户的每个回答都是一条新的 SMS/WhatsApp 消息,并且会再次调用 mybot() 函数/webhook,因此在第二次调用中,您将不会预约看医生incoming_msg 中,但只是一个数字或部门名称。

试试这样:

def mybot():
    incoming_msg = request.values.get('Body', '').lower()
    resp = MessagingResponse()
    msg = resp.message()
    responded = False

    if incoming_msg == "list of doctors":
        msg.body("We have Dr. Phil with us today")
        responded = True

    if "book appointment" in str.lower(incoming_msg) or "see a doctor" in str.lower(incoming_msg):
        msg.body("Which department would you like to visit?\nPress :-\n1 - General Surgery\n2 - Internal Medicine\n3 - Gynaecology\n4 - Obstetrics\n5 - Ophthalmology\n6 - Orthopaedics\n7 - Dermatology Venereology & Leprology\n8 - ENT\n9 - Paediatric")
        responded = True

    if incoming_msg == "1" or "general surgery" in incoming_msg:
        msg.body("General Surgery")
        responded = True

    [...]

只要您的所有选项/输入都不同,这将起作用。

【讨论】:

  • 这实际上是我最初的方法......正如你所说的不同的输入;它会起作用的。但是也有病人登记和付款的问题,这种方法在那里行不通
  • 那么你需要有一个缓存/内存来存储对话的状态。我以前用 DynamoDB 构建过类似的东西,其中电话号码是关键,我将用户在哪个阶段存储在状态列中。 IE。在您的情况下,如果您检查缓存/内存并且找不到任何条目,那么您将处于根级别。然后在缓存/内存中存储status=appointment 之类的东西,下次除了要比较的输入之外,你还会有这个,即 if 语句变为if cache/memory=="appointment" and incoming_msg == "1"...
  • 非常感谢您的提示。我通过设置标志变量做了其他但类似的事情,例如 if incoming_msg == "list of doctor" and flag == 0: or if (incoming_msg == "1" or "general Surgery" in incoming_msg and flag ==1): 和我希望的那样工作
猜你喜欢
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 2013-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-14
相关资源
最近更新 更多