-1

I am creating a Facebook messenger chatbot with pymessenger, flask and wit.at. I want to add buttons as options in my chatbot conversation.

For example

" Generic Offer 1"+"\n"+" Generic Offer 2"+"\n"+" Generic Offer 3"

is what I want to show up as a button to user instead of just text. So the user can select one offer. Facebook has the option to add buttons https://developers.facebook.com/docs/messenger-platform/send-messages/template/button/ via JSON.

I want to do the similar thing. But I don't know how to do it. I can do simple text thing but now this, because I don't know JSON. As of now, my chatbot is replying via bot.send_text_message(sender_id, response).

def webhook():
    data = request.get_json()
    log(data)

    if data['object'] == 'page':
        for entry in data['entry']:
            for messaging_event in entry['messaging']:

                sender_id = messaging_event['sender']['id']
                recipient_id = messaging_event['recipient']['id']

                if messaging_event.get('message'):
                    if 'text' in messaging_event['message']:
                        messaging_text = messaging_event['message']['text']
                    else:
                        messaging_text = 'no text'

                    response = None

                    entity, value = wit_response(messaging_text)

     if entity == 'newstype':
                            response = "OK. I will send you {} news".format(str(value))
                        elif entity == 'cust_greet':
                            response = get_message()
                        elif entity == 'cust_greet2':
                            response = get_message2()
                        elif entity == 'cust_offer':
                            #response = offer_response
                            response = " Generic Offer 1"+"\n"+" Generic Offer 2"+"\n"+" Generic Offer 3"+"\n"+" ️ for more offer enter your cust id"
                            #val_off = test.val_off

        bot.send_text_message(sender_id, response)
finefoot
  • 9,914
  • 7
  • 59
  • 102
codekiller
  • 53
  • 1
  • 9
  • @Jayjayyy: For example: " Generic Offer 1"+"\n"+" Generic Offer 2"+"\n"+" Generic Offer 3" I want to show this text as a button to user. So user can select one offer. Facebook has option to add button (like this https://developers.facebook.com/docs/messenger-platform/send-messages/template/button/) I want to do the similar thing. But I dont know how to do it. I can do simple text thing but now this, because I dont know json. – codekiller Jan 03 '19 at 09:47
  • Hey @Jayjayyy: added. :) – codekiller Jan 03 '19 at 09:59
  • @Jayjayyy: Can you please help. – codekiller Jan 04 '19 at 07:29

1 Answers1

0

I think instead of bot.send_text_message, the correct way to send the JSON payload is via bot.send_raw. So using the example from your link you can test if it's working with something similar to:

payload = """{
  "recipient":{
    "id":"<PSID>"
  },
  "message":{
    "attachment":{
      "type":"template",
      "payload":{
        "template_type":"button",
        "text":"What do you want to do next?",
        "buttons":[
          {
            "type":"web_url",
            "url":"https://www.example1.com",
            "title":"Visit Example 1"
          },
          {
            "type":"web_url",
            "url":"https://www.example2.com",
            "title":"Visit Example 2"
          }
        ]
      }
    }
  }
}"""
bot.send_raw(payload)

Don't forget to replace <PSID>.

finefoot
  • 9,914
  • 7
  • 59
  • 102
  • But What I need to add to pass entries with payload in my code: `if messaging_event.get('message'): if 'text' in messaging_event['message']: messaging_text = messaging_event['message']['text'] else: messaging_text = 'no text' response = None` – codekiller Jan 07 '19 at 04:53