繁体   English   中英

如何在whatsapp中的python,twilio中发送消息并等待回复

[英]how to send a message and wait for a reply in python, twilio in whatsapp

我想在whatsapp上获取一个人的详细信息并将其存储,但它似乎没有按我的意愿工作。 它不会在第二个数据到来之前等待用户输入第一个数据。

这是我的models.py

class student_Account(models.Model):
    name = models.CharField(max_length = 30)
    place_of_stay = models.CharField(max_length = 30)

    def __str__(self):
        return self.name

这是我的views.py

@csrf_exempt
def sms(request):
    incoming_msg = request.POST.get('Body', '').lower()
    resp = MessagingResponse()
    msg = resp.message()
    msg1 = resp.message()

    if 'hi' in incoming_msg:
        reply = ("Hello and welcome to kkk banking system WhatsApp bot!\n\n"
                "What would you like to do\n"
                "1. Create an accout?\n"
                "2. Check your account balance\n")

        msg.body(reply)


    if incoming_msg == '1':
        reply = ("Enter your name")
        a = incoming_msg
        student.name = a
        reply = ("Enter your place of stay")
        b = incoming_msg
        student.place_of_stay = b
        msg.body(reply)
        student.save()
        reply = ("Your details has been saved!!.")
        msg.body(reply)

您的用户通过 WhatsApp/SMS 进行的每个回答/响应都将是对您的sms方法的新调用,即您需要A)拆分逻辑以收集您需要的数据或B)让他们将其输入一个 go。

Re A) :您可以将数据保存在 session、缓存或 model 中,以跟踪用户是否回答了问题(例如,通过用户的电话号码识别,因为每次都提交)。 您将需要找到一种方法来确定答案。 也许通过“输入您的住宿地点”,用户只能从选定数量的答案中进行选择? 然后你可以做一个if检查incoming_msg是否是其中之一,否则它可能是另一个问题的答案,例如伪代码:

if incoming_msg == 'hi':
    # Show welcome message
    # return message
elif incoming_msg == '1':
    # Show first question
    # return message
elif incoming_msg in [<list of available places>]:
    # Retrieve current user from session/cache
    # Save place of stay
    # return message
else:
    # Save name
    # Update session/cache
    # Form second question
    # Return message

Re B) :您可以让用户在一个 go 中提交类似<name> <place of stay>的内容,例如Joseph My place of stay ,然后在业务逻辑中拆分答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM