Pythoncoder
Pythoncoder

Reputation: 69

Facebook Messenger bot Generic Template Not Working

I have created a Facebook Messenger bot which works fine. I have used the Button Template and Image template, and both work perfectly. But when I try the Generic Template, I get no response. I have simply copy pasted the code from here, by performing the appropriate modifications.

I don't know how to debug. Facebook Messenger gives no output on the messaging box. I am currently running the app via Heroku.

Here is my code:

def send_message(token, recipient):
    r = requests.post("https://graph.facebook.com/v2.6/me/messages",
     params={"access_token": token},
     data=json.dumps({
      "recipient":{
        "id":recipient
      },
      "message":{
        "attachment":{
          "type":"template",
          "payload":{
            "template_type":"generic",
            "elements":[
               {
                "title":"Welcome to Peter\'s Hats",
                "image_url":"http://www.godominion.com/content/images/feature-img-small-appliance-electronics.png",
                "subtitle":"We\'ve got the right hat for everyone.",
                "default_action": {
                  "type": "web_url",
                  "url": "https://peterssendreceiveapp.ngrok.io/view?item=103",
                  "messenger_extensions": true,
                  "webview_height_ratio": "tall",
                  "fallback_url": "https://peterssendreceiveapp.ngrok.io/"
                },
                "buttons":[
                  {
                    "type":"web_url",
                    "url":"https://petersfancybrownhats.com",
                    "title":"View Website"
                  }           
                ]      
              }
            ]
          }
        }
      }
    }),
     headers={'Content-type': 'application/json'})
    if r.status_code != requests.codes.ok:
      print r.text 

I would appreciate any help.

Thank you.

EDIT 1: SOLUTION

I got rid of the issue by commenting out:

"messenger_extensions": true,

and

"fallback_url": "https://peterssendreceiveapp.ngrok.io/"},

I'm sure this is not the correct method. But as I am creating a bot, without actual links, this works.

Upvotes: 1

Views: 1825

Answers (2)

user10538706
user10538706

Reputation: 71

Try like this firstly make a function

def function():

 extra_data = {

            "attachment": {
                "type": "template",
                "payload": {
                    "template_type": "generic",
                    "elements": [
                        {
                            "title": "Any Title",
                            "image_url": "https://mbtskoudsalg.com/images/road-clipart-journey-3.png",
                            "subtitle": "Subtitle.",
                            "buttons": [
                                {
                                    "type": "web_url",
                                    "title": "View",
                                    "url": "**MAKE SURE TO WHITELIST THIS URL**", # URL
                                    "messenger_extensions": "true",
                                    "webview_height_ratio": "full"
                                }
                            ]
                        }
                    ]
                }
            }
        }
    # w_message = "Hi there! How may I help you?"

    fb_message_template(extra_data["attachment"], "****RECIEVER ID****")

Make another function

import requests

# // Importing User Defined Modules // #
from get_environ_var import get_environ_var

# // Global vars // #
ACCESS_TOKEN = "FB_ACCESS_TOKEN"


def fb_message_template(extra_data, sender_id):
    """This function sends template message to facebook"""

    data = {
        'recipient': {'id': sender_id},
        'message': {
            "attachment": extra_data
        }
    }

    qs = 'access_token=' + ACCESS_TOKEN

    resp = requests.post('https://graph.facebook.com/v2.6/me/messages?' + qs, json=data)

    print(resp.content)

Upvotes: 1

Sandesh B Suvarna
Sandesh B Suvarna

Reputation: 771

On the second button, "url":"https://petersfancybrownhats.com" is broken.

Upvotes: 1

Related Questions