0

I'm having trouble wrapping my head around how exactly to use the Facebook Graph Api and Webhooks. I'm trying to send and read messages from a group chat that I'm a part of on Facebook. I followed the steps to create an app on Developers.Facebook.com and passed the app review for pages_messages. I generated a Page_access_token and subscribed my app to my fb page. I imagine I need to give Facebook the actual page_access_token and app_secret somewhere in my code, but the tutorial I followed says nothing about it. I'm using django, and the following code to receive the webhook from Facebook using a tutorial I found online:

class botView(generic.View):
def get(self, request, *args, **kwargs):
    if self.request.GET['hub.verify_token'] == VERIFY_TOKEN:
        print(self.request.GET['hub.challenge'])
        return HttpResponse(self.request.GET['hub.challenge'], 200)
    else:
        return HttpResponse('Error, invalid token')

@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
    return generic.View.dispatch(self, request, *args, **kwargs)

# Post function to handle Facebook messages
def post(self, request, *args, **kwargs):
    # Converts the text payload into a python dictionary
    incoming_message = json.loads(self.request.body.decode('utf-8'))
    print(incoming_message)
    # Facebook recommends going through every entry since they might send
    # multiple messages in a single call during high load
    for entry in incoming_message['entry']:
        for message in entry:
            pass
            # Check to make sure the received call is a message call
            # This might be delivery, optin, postback for other events
            if 'message' in message:
                # Print the message to the terminal
                pprint(message)
                # Assuming the sender only sends text. Non-text messages like stickers, audio, pictures
                # are sent as attachments and must be handled accordingly.
                #post_facebook_message(message['sender']['id'], message['message']['text'])

    return HttpResponse("ok", 200)

Here is the link to the tutorial

https://abhaykashyap.com/blog/post/tutorial-how-build-facebook-messenger-bot-using-django-ngrok

I can send tests from the webhooks to my server and get responses like the following:

{'entry': [{'time': 1535460973, 'id': '0', 'changed_fields': ['message_sends'], 'uid': '0'}], 'object': 'user'

But the samel response that is provided by the webhook looks like:

    {
  "field": "message_sends",
  "value": {
    "id": "mid.86753098675309:uuddlrlrbas",
    "message": "I'm changing jobs for the next 4+ years. Wanna be CEO?",
    "from": {
      "id": 44444444,
      "name": "Lizard Zuckerman",
      "email": "zuck@tehfacebook.com"
    },
    "to": {
      "data": [
        {
          "id": 20061985,
          "name": "Bobbert Cruz",
          "email": "bobbert@tehfacebook.com"
        }
      ]
    }
  }
}

Ngrok does show that there was a successful connection when I receive tests, but I get nothing when I receive messages on my page, even though I'm subscribed to it.

juju
  • 884
  • 1
  • 9
  • 31
  • What exactly do you mean by “group chat”? Messenger API is for communication between a bot (on behalf of a page), and a single user. – misorude Aug 29 '18 at 07:19
  • I see. I seem to be looking into the wrong thing entirely. A group chat on messenger, or my normal inbox. I want to send automated messages to a people that are a part of a group chat that I am associated with – juju Aug 29 '18 at 19:27
  • There is no API for that. – misorude Aug 30 '18 at 06:32

0 Answers0