【问题标题】:how to send a message and wait for a reply in python, twilio in whatsapp如何在python中发送消息并等待回复,whatsapp中的twilio
【发布时间】:2021-04-27 18:40:12
【问题描述】:

我想在 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)

【问题讨论】:

  • 我建议你使用渠道进行实时交流

标签: python django twilio bots


【解决方案1】:

您的用户通过 WhatsApp/SMS 做出的每个回答/回复都将是对您的 sms 方法的新调用,即您需要A) 拆分逻辑以收集您需要的数据,或者B)让他们一口气输入。

Re A):您可以将数据保存在会话、缓存或模型中,以跟踪用户是否回答了问题(例如,通过用户的电话号码识别,因为这是提交的每一次)。您将需要找到一种方法来确定答案。也许通过“输入您的住宿地点”,用户只能从选定数量的答案中进行选择?然后你可以做一个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):您可以让用户一次性提交&lt;name&gt; &lt;place of stay&gt; 之类的内容,例如Joseph My place of stay 然后你在业务逻辑中拆分答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-13
    • 2023-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    相关资源
    最近更新 更多