我可以获取聊天机器人询问的用户消息吗?

时间:2019-10-24 15:26:33

标签: facebook-chatbot

最近,我制作了一个fb聊天机器人,我使用post_back按钮让选择器使用,但是我想记录用户的答案,我设置了6个按钮来询问他们。我的问题是可以获取用户选择的每个按钮并将这些消息传递给MySQL吗?

我已经尝试了一些代码,但是没有用!

buttons = [
  Template.ButtonPostBack("Jacket", "DEVELOPED_DEFINED_PAYLOAD"),
  Template.ButtonPostBack("TOPS", "DEVELOPED_DEFINED_PAYLOAD"),
  Template.ButtonPostBack("PANTS", "DEVELOPED_DEFINED_PAYLOAD"),
]

buttons2 = [
  Template.ButtonPostBack("GAP",  "test"),
  Template.ButtonPostBack("NIKE", "test"),
  Template.ButtonPostBack("ELSE", "test"),
]

@page.handle_message
def message_handler(event):

    recipient_id = event.sender_id

    message = event.message.get('text')

    page.send(recipient_id, Template.Buttons("Please choose Type", 
buttons))


@page.callback(['DEVELOPED_DEFINED_PAYLOAD'])
def callback_clicked_button(payload, event):
    recipient_id = event.sender_id
    page.send(recipient_id, Template.Buttons("Please choose 
brand",buttons2))
    db_insert('DEVELOPED_DEFINED_PAYLOAD')

connection = pymysql.connect(host='localhost',
                             user='myacount',
                             password='mypassword',
                             db='list',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)    


def db_insert(data):

    try:
        with connection.cursor() as cursor:
            sql = INSERT INTO chatbot (type, brand) VALUES (%s, 
%s)
            cursor.execute(sql, (Jacket,data)) 

    finally:    

我想从用户那里获取答案

0 个答案:

没有答案