0

I have created a messenger chatbot with flask, pymessenger and wit.ai.

I want to add facebook provided templates (like buttons, adding images and sound media)(https://developers.facebook.com/docs/messenger-platform/reference/template/button/)

There using some curl and json thing which I do not understand. Can some one help me, where should I put these snippet in my python code.

    @app.route('/', methods=['POST'])

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_bye':
                    response = "Bye! Have a Good Day!".format(str(value))
                elif entity == 'cust_option':
                    response = "Option 1: Option 2:"
                bot.send_text_message(sender_id, response)


return "ok", 200

def log(message): print(message) sys.stdout.flush()

codekiller
  • 53
  • 1
  • 9

1 Answers1

1

HTTP requests use one of these two formats:

GET: All the request information is in the url

POST: Some information is sent via a JSON format to the url

What we see in the Facebook API is a POST request: the url is defined as

https://graph.facebook.com/v2.6/me/messages?access_token=<PAGE_ACCESS_TOKEN>

...and there is POST request information in the JSON section underneath

Curl is a program used to send HTTP requests from the terminal. If you install Curl, you can fill in the JSON/url information, run the command (which sends the POST request), and see the buttons pop up for the recipient. Just as you want your chatbot to do!

However, Curl is a tool, not a Python library!

To do this in Python, you can send the request through Python's built in libraries, or install a package which makes this easier (such as requests), look into "sending http requests via python".

Below is an example (adapted from this question):

    from urllib.parse import urlencode
    from urllib.request import Request, urlopen

    # the url we are sending the request to
    url = "https://graph.facebook.com/v2.6/me/..."

    # the POST request data
    request_data = {
                     "recipient": {
                       "id": "<PSID>"
                     },
                     "message": {
                       "attachment": {
                           ...
                       }
                     }
                   }

    # create the request with the url and the data
    post_request = Request(url, urlencode(request_data).encode())

    # send it to Facebook! Response is the API response from Facebook
    response = urlopen(post_request).read().decode()
Jack
  • 380
  • 1
  • 3
  • 12
  • Thanks for quick response. I have added my code for response, suppose I want to add option1 and option2 as button, Can you help me with code. – codekiller Dec 26 '18 at 09:32
  • Your current code is using the send_text_message() function to send the message, which behind the scenes sends a POST request. That won't work for the button because the post requests are structured different. You'll need to make a separate function that sends the POST request with the JSON structure specified by the Facebook API website. – Jack Dec 26 '18 at 09:47
  • Hey Jack thanks. Can you give me an example with mine code posted above, really new this. – codekiller Dec 26 '18 at 10:27
  • 1
    I'm not going to write your code for you, but I did put an example using your code in the answer. You can adapt that code into a function or put it inside the if statement. – Jack Dec 27 '18 at 00:33