0

I'm making an automated whatsapp reply bot using Twilio and python, however I am facing problems and am unable to used nested if else in it

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

where if (incoming_msg == "1" or "general surgery" in incoming_msg): is the problem statement.

Is there any way to work this out?

yvesonline
  • 4,609
  • 2
  • 21
  • 32
JS_Rudra
  • 5
  • 5
  • 1
    What makes you think it's getting ignored? Did you `print(incoming_msg)` immediately before it? What was the output? – Axe319 Apr 07 '21 at 12:42
  • @Axe319 I used a print("HI") statement inside the nested if but it wasn't printed in the console. Also, here is the console log in case... 127.0.0.1 - - [07/Apr/2021 18:15:40] "POST /mybot HTTP/1.1" 200 - see a doctor 127.0.0.1 - - [07/Apr/2021 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 – JS_Rudra Apr 07 '21 at 12:47
  • Can you put a `print(incoming_msg)` before and after this line `if "book appointment" in str.lower(incoming_msg) or "see a doctor" in str.lower(incoming_msg):` and post the output? – Axe319 Apr 07 '21 at 13:48
  • @Axe319 127.0.0.1 - - [07/Apr/2021 20:28:53] "POST /mybot HTTP/1.1" 200 - see a doctor see a doctor 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 - general surgery – JS_Rudra Apr 07 '21 at 14:59
  • There's your problem. The first `incoming_msg` is `'see a doctor'` which won't pass your inner `if` statement and your second one is `'general surgery'` which won't pass the outer `if`. – Axe319 Apr 07 '21 at 15:52
  • @Axe319 Thank you, now that you've put the problem into words, it became a lot clearer. I did try to initialize input variable inside if statement but somehow that also doesnt seem to work – JS_Rudra Apr 07 '21 at 17:02

1 Answers1

1

In this case you can't nest them. Each answer by a user is a new SMS/WhatsApp message and will call the mybot() function/webhook again, hence in the second call you won't have book appointment or see a doctor in the incoming_msg but just a number or the name of the department.

Try it like this:

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

    [...]

As long as all of your options/inputs are distinct this will work.

yvesonline
  • 4,609
  • 2
  • 21
  • 32
  • That was actually my original approach...and as you said about distinct inputs; it will work. But there is also the problem of making patient registration and payments, stuff like that and this method won't work there – JS_Rudra Apr 08 '21 at 16:32
  • Then you'll need to have a cache/memory where you store the state of the conversation. I've build something like this before with DynamoDB where the telephone number was the key and I stored at what stage the user is in a status column. I.e. in your case if you check the cache/memory and you find no entry you would be at the root level. Then store something like `status=appointment` in the cache/memory and next time around you'll have this in addition to the input to compare, i.e. the if-statement becomes `if cache/memory=="appointment" and incoming_msg == "1"...`. – yvesonline Apr 08 '21 at 18:08
  • thank you very much for your hint. I did something else but similar in this context by setting a flag variable, such as if incoming_msg == "list of doctors" and flag == 0: or if (incoming_msg == "1" or "general surgery" in incoming_msg and flag==1): and its working as I wanted it to be – JS_Rudra Apr 09 '21 at 16:38