【发布时间】: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