Have you read TechCrunch's article? Didn't you read? Then this? Also here or here / facebook-messenger-ai /) or here or [here](http://qiita.com/naoyashiga / items / 6c663f6e773490934312). Articles are increasing rapidly. This is all articles from 4/13. If you haven't read it, please read it. And if you don't know it, you can be impressed. ** You can now turn Facebook Messenger into a bot. ** ** You can do it in no time with a little tweaking of the Facebook API.
If you read here, everything is written, but if you read English, it will be explained appropriately for people who have urticaria. I will. That said, there is little to do. So I could understand too. By the way, this time it is implemented with python × Tornado.
Let's look at them in order
I made it really properly. This time it's a bot trial. App category? I think WWW is fine. After making it, the setting screen of the application? Please go to. Second from the bottom. Messenger-> Start
There is a place to select a Facebook page, so set the page you created earlier. You will get an access token for the page.
What is a webhook? Did you not know it, it seems that it is a function that allows you to hit an arbitrary URL when something changes (http://blog.manaten.net/entry/573) When a message comes to the messenger who turned into a bot, it seems to post to the set URL. So, first you have to authenticate the URL.
For the time being, I wrote a program that processes authentication first. Use Tornado with python. Apparently there are many 5000 ports, so 5000. FB official had a process for js, so I fixed it to python.
server.py
# -*- coding: utf-8 -*-
import tornado.ioloop
import tornado.web
import json
verify_token = <VERIFY_TOKEN>
class WebHookHandler(tornado.web.RequestHandler):
def get(self):
if self.get_argument("hub.verify_token", "") == verify_token:
self.write(self.get_argument("hub.challenge", ""));
else:
self.write('Error, wrong validation token');
application = tornado.web.Application([
(r"/webhook", WebHookHandler)
])
if __name__ == "__main__":
application.listen(5000)
tornado.ioloop.IOLoop.instance().start()
Write up to this point and confirm that it starts up on port 5000. Let's set the URL for the webhook.
In the URL [http: // localhost: 5000 / webhook](http: // localhost: 5000 / webhook), the token is for authentication, so anything is fine, but make it the same value as \ <VERIFY_TOKEN> used in the python variable. please. This time I chose "test_fb". Check and save the follow input field as you like ... ···can not. It is said to be the https one.
What should I do? I just want to use it for a while, but it's tedious to give it to the server. [https: // localhost: 5000](https: // localhost: 5000) doesn't work (it didn't work).
There is a super useful application called ngrok in the world.
The great thing is that you can temporarily expose one port of your localhost to the outside world. Moreover, it also issues an https URL. Great.
Mac users can enter with brew install ngrok. I entered, but I didn't go well, so I obediently dropped it from ngrok official.
The usage is sure to follow → here. Official strongest.
./ngrok http 5000
Hit
If such a thing comes out, you win. The address to be forwarded (ca27a10a part) is assigned differently every time, but if you just try it for the time being, there should be no problem.
Set the https address assigned to the URL of the webhook earlier. You should be able to save this time.
It seems that authentication is required before sending and receiving.
curl -ik -X POST "https://graph.facebook.com/v2.6/me/subscribed_apps?access_token=<PAGE_ACCESS_TOKEN>
\ <PAGE_ACCESS_TOKEN> is an access token for facebook page.
{"success":true}
This is what comes out. Ready when it comes out ok
FB Official has sample code for js. As you can see, when a message comes, the post will fly to the URL of the webhook. On the other hand, when sending a message, post it to https://graph.facebook.com/v2.6/me/messages. If you get here, all you have to do is write the code. I will post the code I wrote for the time being. \ <VERIFY_TOKEN> is the URL for webhook authentication. This time "test_fb". \ <PAGE_ACCESS_TOKEN> is an access token for facebook page as above.
server.py
#!/bin/env python
# -*- coding: utf-8 -*-
import tornado.ioloop
import tornado.web
import json
import requests
verify_token = <VERIFY_TOKEN>
token = <PAGE_ACCESS_TOKEN>
def sendTextMessage(sender, text):
if len(text) <= 0:
return
url = 'https://graph.facebook.com/v2.6/me/messages'
headers = {'content-type': 'application/json'}
data = {
"recipient": {
"id":sender
},
"message": {
"text":"echo: " + text
}
}
params = {"access_token":token}
r = requests.post(url, params=params, data=json.dumps(data), headers=headers)
#print r.text
class WebHookHandler(tornado.web.RequestHandler):
def get(self):
if self.get_argument("hub.verify_token", "") == verify_token:
self.write(self.get_argument("hub.challenge", ""));
else:
self.write('Error, wrong validation token');
def post(self):
print "receive!"
data = json.loads(self.request.body)
print data
messaging_events = data["entry"][0]["messaging"]
text = ""
for event in messaging_events:
sender = event["sender"]["id"];
if ("message" in event and "text" in event["message"]):
text = event["message"]["text"];
sendTextMessage(sender, text)
application = tornado.web.Application([
(r"/webhook", WebHookHandler)
])
if __name__ == "__main__":
application.listen(5000)
tornado.ioloop.IOLoop.instance().start()
The content of the program is extremely simple. When a message flies, add "echo:" to it and return it. that's all. With this alone (for the time being), you can create a bot. I just get back what I hit. But it's fun.
that's all. Thank you for your hard work. Please send us a fun Bot Life.
Recommended Posts