簡體   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