ABSGINC
You can call me Scott.
- Joined
- Oct 1, 2014
- Messages
- 59
- Reaction score
- 31
Want PROOF of CONCEPT? Here it is.
We've got SMS alerts, EMAIL alerts, but what about Facebook Messenger alerts?
Better yet, WHY Facebook Messenger alerts?
ALMOST everyone communicates with Facebook Messenger, secondly with their well documented API you can send nice buttons, lists, and all kinds of usefulness and build an awesome BOT for your communications platform.
In this example i'm using this to only communicate with me and other staff members, if you build a thorough application and submit for review you can communicate with FB users who aren't admins or moderators to the page you created.
One benefit to this, you can send a Facebook message to a phone number and Facebook will attempt to deliver it to a Facebook account registered at that phone #.
Why would you want to do that? -- how about after a call leaves your system, maybe you have an app setup to ask for a customer service review, after hanging up your caller could receive a facebook message from your company thanking them for their call and asking for their feedback. Your customer might not have known you even had a facebook page, and you might not have known your customer did either! -
In moving forward I imagine my voip server communicating with me through my Facebook Messenger Bot, listing me my voicemail in a visual list, listing me my missed calls, passing me the audio, yes this is all possible as I look through the documentation, and if I had enough caffeine and adrenaline, I could probably finish it all this year ;-) I'm serious, take a look at the API reference and how simple it is to make these requests with some simply json formatting and a CURL PUSH.
I want to share with you what I have so far, IT WORKS
You'll probably need to get your feet wet with this Facebook API python bot example I checked out here first, this is where I gained some momentum. GO HERE: django ngrok facebook bot example on python
The example explains how to register your FB page, application and webhooks. Facebook only will pass these webhooks via https and if your wazo is behind a firewall as well no need to worry, ngrok is a great solution and it is used in the linked example to create a tunnel to your localhost via a defined DNS A record supplied by ngrok. The example also uses django to host the server and handle the requests between facebook and our app.
I have a few windows open here to show you what happens when I call into my application. In the TOP IMAGE; the top right window is the asterisk console, the top left is the django server console for the webhook callbacks that come in, this is necessary to handle responses down the road, right now the example simply echos back "I think you said XXX"
The call comes in and I execute a file similar to how wolfram alpha executes 4747
Except I execute 52637 (also located in the asterisk agi-bin)
It sends a CURL POST and as the call comes in I get a Desktop Notification from Facebook immediately, also my tablet and mobile phone alerted of Facebook messages
And take a look at this custom formatted message shown on my mobile!
I can press ANSWER or IGNORE and send this payload back to my wazo server!
I receive a message that said a call entered the 52637 application but have not passed the caller ID yet. The message is also sent with 2 quick reply buttons that allow a payload to be sent back to the bot on wazo
Later in development that choice which could simply update a flat text file, a variable, and throughout the dialplan application progress that status can be checked to see if the call should be forwarded to the individual who pressed answer.. ----pretty neat?
Let me show you how I sent this button,
here is the code for 52637
and when I route a call to start this process, this is what it looks like
So as you can see in the dialplan code, i commented out some lines of trying CURL right from the dialplan, couldnt get the post right, so I ran it in command line style shown above, and as i showed you in the images, it works. When I call in my application I get a Facebook Message from the page I created telling me there is a caller in the system. Just at that I'm thrilled.
There are other python files created and edited as I followed the tutorial for basically building a bot, these files basically setup the url and tokens for registering the application with facebook and for handling the events.
main file being views.py - This guy echos back when i type to it from my facebook account, he could easily do other actions on these hooks like update variables that could effect the dialplan, or do other actions and reply back with fetched data like responding to commands like last 10 calls, voicemail, call recordings, etc. But for now, lets echo.
I'm really really tired. This wasn't all exactly smooth sailing for me, and I've learned a lot, just today.
But I don't want to stop there, so I need to get better at this asterisk fun stuff.
Who can offer me some tips on making this really WORK.
What I mean is I set this up on a virtual environment, this will never survive a reboot. But with this PROOF OF CONCEPT IN PLACE, who here got excited with me?
I'd like a put something together on gitbuh that could be cloned to include all of the madness above and survive a reboot.. There is a lot of directions we could go from here. But here we are.
SUCCESSFUL FACEBOOK MESSENGER COMMUNICATION!
Comments, Questions, Tips, Tricks, I need some feedback please!
We've got SMS alerts, EMAIL alerts, but what about Facebook Messenger alerts?
Better yet, WHY Facebook Messenger alerts?
ALMOST everyone communicates with Facebook Messenger, secondly with their well documented API you can send nice buttons, lists, and all kinds of usefulness and build an awesome BOT for your communications platform.
In this example i'm using this to only communicate with me and other staff members, if you build a thorough application and submit for review you can communicate with FB users who aren't admins or moderators to the page you created.
One benefit to this, you can send a Facebook message to a phone number and Facebook will attempt to deliver it to a Facebook account registered at that phone #.
Why would you want to do that? -- how about after a call leaves your system, maybe you have an app setup to ask for a customer service review, after hanging up your caller could receive a facebook message from your company thanking them for their call and asking for their feedback. Your customer might not have known you even had a facebook page, and you might not have known your customer did either! -
In moving forward I imagine my voip server communicating with me through my Facebook Messenger Bot, listing me my voicemail in a visual list, listing me my missed calls, passing me the audio, yes this is all possible as I look through the documentation, and if I had enough caffeine and adrenaline, I could probably finish it all this year ;-) I'm serious, take a look at the API reference and how simple it is to make these requests with some simply json formatting and a CURL PUSH.
I want to share with you what I have so far, IT WORKS
You'll probably need to get your feet wet with this Facebook API python bot example I checked out here first, this is where I gained some momentum. GO HERE: django ngrok facebook bot example on python
The example explains how to register your FB page, application and webhooks. Facebook only will pass these webhooks via https and if your wazo is behind a firewall as well no need to worry, ngrok is a great solution and it is used in the linked example to create a tunnel to your localhost via a defined DNS A record supplied by ngrok. The example also uses django to host the server and handle the requests between facebook and our app.
I have a few windows open here to show you what happens when I call into my application. In the TOP IMAGE; the top right window is the asterisk console, the top left is the django server console for the webhook callbacks that come in, this is necessary to handle responses down the road, right now the example simply echos back "I think you said XXX"
The call comes in and I execute a file similar to how wolfram alpha executes 4747
Except I execute 52637 (also located in the asterisk agi-bin)
It sends a CURL POST and as the call comes in I get a Desktop Notification from Facebook immediately, also my tablet and mobile phone alerted of Facebook messages
And take a look at this custom formatted message shown on my mobile!
I can press ANSWER or IGNORE and send this payload back to my wazo server!
I receive a message that said a call entered the 52637 application but have not passed the caller ID yet. The message is also sent with 2 quick reply buttons that allow a payload to be sent back to the bot on wazo
Later in development that choice which could simply update a flat text file, a variable, and throughout the dialplan application progress that status can be checked to see if the call should be forwarded to the individual who pressed answer.. ----pretty neat?
Let me show you how I sent this button,
here is the code for 52637
Code:
curl -X POST -H "Content-Type: application/json" -d '{
"recipient":{
"id":"<This is the facebook id i want this alert sent to i found it in the logs when i messaged myself>"
},
"message":{
"text": "A call just entered the 52637 application the caller ID is: I havent got that far in code yet!",
"quick_replies":[
{
"content_type":"text",
"title":"ANSWER",
"payload":"ANSWER CALL",
},
{
"content_type":"text",
"title":"IGNORE CALL",
"payload":"IGNORE CALL"
}
]
}
}' "https://graph.facebook.com/v2.6/me/messages?access_token=<this is my huge access token that i wont be sharing here>"
and when I route a call to start this process, this is what it looks like
Code:
[call-landron] ; Give Landron a call
exten => s,1,Answer
exten => s,n,Wait(1)
exten => s,n,System(/var/lib/asterisk/agi-bin/52637) ; THIS IS WHERE THE ABOVE CODE EXECUTES
;exten => s,n,Set(CURLOPT(ssl_verifypeer)=0) ; i couldnt get the curl to work from here
;exten => s,n,Set(CURL_RESULT=${CURL(https://graph.facebook.com/v2.6/me/messages?access_token=<get_your_own_access_token>,/var/lib/asterisk/agi-bin/alertfb.json)}) ; I tried the json in a file
;exten => s,Set(result=${JSONELEMENT(CURL_RESULT,error/message)})
;exten => s,Verbose(${result)
exten => s,n,Playback(custom/qualitytrain)
exten => s,n,Dial(Local/52637@default)
So as you can see in the dialplan code, i commented out some lines of trying CURL right from the dialplan, couldnt get the post right, so I ran it in command line style shown above, and as i showed you in the images, it works. When I call in my application I get a Facebook Message from the page I created telling me there is a caller in the system. Just at that I'm thrilled.
There are other python files created and edited as I followed the tutorial for basically building a bot, these files basically setup the url and tokens for registering the application with facebook and for handling the events.
main file being views.py - This guy echos back when i type to it from my facebook account, he could easily do other actions on these hooks like update variables that could effect the dialplan, or do other actions and reply back with fetched data like responding to commands like last 10 calls, voicemail, call recordings, etc. But for now, lets echo.
Code:
import json, requests, random, re
from pprint import pprint
from django.views import generic
from django.http.response import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
def post_facebook_message(fbid, recevied_message):
post_message_url = 'https://graph.facebook.com/v2.6/me/messages?access_token=<put-your-token-here>'
response_msg = json.dumps({"recipient":{"id":fbid}, "message":{"text":recevied_message}})
status = requests.post(post_message_url, headers={"Content-Type": "application/json"},data=response_msg)
pprint(status.json())
# Create your views here.
class AlertFBView(generic.View):
def get(self, request, *args, **kwargs):
if self.request.GET['hub.verify_token'] == '<yourowncodeheretoo>':
return HttpResponse(self.request.GET['hub.challenge'])
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'))
# 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['messaging']:
# 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)
sendthis = "I think you said " + message['message']['text']
pprint('this is what i sent')
pprint(sendthis)
post_facebook_message(message['sender']['id'], sendthis)
return HttpResponse()
I'm really really tired. This wasn't all exactly smooth sailing for me, and I've learned a lot, just today.
But I don't want to stop there, so I need to get better at this asterisk fun stuff.
Who can offer me some tips on making this really WORK.
What I mean is I set this up on a virtual environment, this will never survive a reboot. But with this PROOF OF CONCEPT IN PLACE, who here got excited with me?
I'd like a put something together on gitbuh that could be cloned to include all of the madness above and survive a reboot.. There is a lot of directions we could go from here. But here we are.
SUCCESSFUL FACEBOOK MESSENGER COMMUNICATION!
Comments, Questions, Tips, Tricks, I need some feedback please!