Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Introductions of Messaging bot 做聊天機器人

3.099 Aufrufe

Veröffentlicht am

想做聊天機器人?這裡可以給你一些指引
包含 Slack, Line , FB Messenger, Skype 的串接

Veröffentlicht in: Technologie
  • Login to see the comments

Introductions of Messaging bot 做聊天機器人

  1. 1. M E S S A G I N G B O T I N T R O D U C T I O N S O F JOHNNY SUNG 2016.06.18 @ GDG Kaohsiung
  2. 2. MOBILE DEVICES DEVELOPER Johnny Sung https://fb.com/j796160836 https://plus.google.com/+JohnnySung http://about.me/j796160836
  3. 3. http://i57.tinypic.com/2wfo8rl.png
  4. 4. http://cdn1.bigcommerce.com/server3400/v4eyu8t/products/1722/images/2350/Essential_NPC_1__63835.1427321112.1280.1280.jpg
  5. 5. http://www.jeanchristophebonis.com/wp-content/uploads/2015/01/5949_render_walle4.png
  6. 6. • • Git commit, pull request • Server deploy • • • …
  7. 7. • ( ) • (?) • • • …
  8. 8. 
 http://www.ettoday.net/news/20141231/445744.htm
  9. 9. https://www.facebook.com/DoctorKoWJ/photos/a.415511451884174.1073741827.136845026417486/574861115949206
  10. 10. S L A C K
  11. 11. S L A C K • Incoming Webhooks • Send messages into Slack with customization and rich formatting. • Outgoing Webhooks • Send messages from Slack to your service in real-time. • Bot Users • Connect to Slack to read and write as a real-time bot.
  12. 12. I N C O M I N G W E B H O O K S https://api.slack.com/incoming-webhooks Your server Slack POST 200 OK
  13. 13. curl -X POST -H 'Content-type: application/json' --data ‘{“text”:"Hello, World!”}’ https://hooks.slack.com/services/T00000000/B00000000/ XXXXXXXXXXXXXXXXXXXXXXXX I N C O M I N G W E B H O O K S https://api.slack.com/incoming-webhooks
  14. 14. I N C O M I N G W E B H O O K S https://api.slack.com/incoming-webhooks
  15. 15. • Trigger keyword(s) • Callbacks • Cons: Public channel only O U T G O I N G W E B H O O K S https://api.slack.com/outgoing-webhooks
  16. 16. O U T G O I N G W E B H O O K S https://api.slack.com/outgoing-webhooks Slack 1. Detect if have certain keyword Bonjour 안녕하세요 สวัสดี नमस्ते မဂ#လ%ပ'
  17. 17. O U T G O I N G W E B H O O K S https://api.slack.com/outgoing-webhooks Slack Your Server 200 OK 2. POST user message POST
  18. 18. O U T G O I N G W E B H O O K S https://api.slack.com/outgoing-webhooks Slack Your Server 200 OK POST 3. Return bot message
  19. 19. B O T U S E R S ( R E A L T I M E M E S S A G I N G A P I ) https://api.slack.com/bot-users Your server Slack WebSocket
  20. 20. https://api.slack.com/bot-users Your server Slack WebSocket Hello User Online User Typing Messages… B O T U S E R S ( R E A L T I M E M E S S A G I N G A P I )
  21. 21. https://api.slack.com/bot-users Your server Slack WebSocket Hello User Online User Typing Messages… B O T U S E R S ( R E A L T I M E M E S S A G I N G A P I )
  22. 22. https://api.slack.com/rtm More info:
  23. 23. https://api.slack.com/bot-users Your server Slack WebSocket Post Messages Bots Info Users Info… B O T U S E R S ( R E A L T I M E M E S S A G I N G A P I )
  24. 24. https://api.slack.com/methods More info:
  25. 25. https://github.com/slackhq/python-slackclient * pip required
  26. 26. #!/usr/bin/python
 # -*- coding: utf-8 -*-
 
 import time
 from slackclient import SlackClient
 # Token found at https://api.slack.com/web#authentication
 token = "xoxb-52095439187-CYPqixxxxxzwR9MoriR"
 sc = SlackClient(token)
 if sc.rtm_connect():
 while True:
 results = sc.rtm_read()
 for result in results:
 if 'type' in result:
 if result['type'] == u'message' and not('subtype' in result):
 channel = result['channel']
 text = result['text']
 user = result['user']
 if channel != u'C02HKPG5U' and user != u'U1J2TCX5H': # Filter out:
 # C02HKPG5U ==> General channel
 # U07SJTAMC ==> Bot it's self
 try:
 sc.rtm_send_message(channel, text)
 except Exception, e:
 print e 
 print result
 time.sleep(5)
 else:
 print "Connection Failed, invalid token?" Python
  27. 27. {u'text': u’Hello, world!’, u'ts': u'1466228461.000005', u'user': u'U02HKPG5G', u'team': u'T02HKPG5C', u'type': u'message', u'channel': u'G1J4EADCH'} {u'type': u'hello'} {u'type': u'presence_change', u'user': u'U1J4C9FGF', u'presence': u'active'}
  28. 28. L I N E
  29. 29. https://developers.line.me/bot-api/overview
  30. 30. SSL Certificate
  31. 31. https://developers.line.me/bot-api/getting-started-with-bot-api-trial Your server Line L I N E B O T Messages https://
  32. 32. https://developers.line.me/bot-api/getting-started-with-bot-api-trial Your server Line Post Messages L I N E B O T
  33. 33. https://developers.line.me/bot-api/getting-started-with-bot-api-trial
  34. 34. POST /linebot.php HTTP/1.1 ACCEPT: */* ACCEPT-CHARSET: utf-8 CONTENT-TYPE: application/json;charset=UTF-8 USER-AGENT: ChannelEventDispatcher/1.0 X-LINE-CHANNELSIGNATURE: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx CONTENT-LENGTH: 542 HOST: your.ip.here:443 CONNECTION: Keep-Alive ACCEPT-ENCODING: gzip,deflate {“result”:[{"content":{"toType":1,"createdTime": 1465701674031,"from":"u2832cfc217c72c6df812248d041b73c8","location":null,"id":"4450389 514583","to":["uc6b4b8ef65ce03b72a672ac34b6e3a60"],"text":"Hello, World!”,”contentMetadata": {"AT_RECV_MODE":"2","EMTVER":"4","SKIP_BADGE_COUNT":"true"},"deliveredTime": 0,"contentType":1,"seq":null},"createdTime": 1465701674063,"eventType":"138311609000106303","from":"u206d25c2ea6bd87c17655609a 1c37cb8","fromChannel":1341301815,"id":"WB1521-3505347188","to": ["uc6b4b8ef65ce03b72a672ac34b6e3a60"],"toChannel":1463624852}]} https://developers.line.me/bot-api/getting-started-with-bot-api-trial
  35. 35. https://developers.line.me/bot-api/getting-started-with-bot-api-trial
  36. 36. <?php
 function sendMessage($toIdArr, $message)
 {
 $curl = curl_init();
 $options = array(
 CURLOPT_URL => "https://trialbot-api.line.me/v1/events",
 CURLOPT_HEADER => 0,
 CURLOPT_VERBOSE => 0,
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_POST => true,
 CURLOPT_POSTFIELDS => array(
 "to" => $toIdArr,
 "toChannel" => 1383378250,
 "eventType" => "138331608800106203",
 "content" => array(
 "contentType" => 1,
 "toType" => 1,
 "text" => $message
 ),
 ),
 CURLOPT_HTTPHEADER => array(
 'X-Line-ChannelID: 1463624852',
 'X-Line-ChannelSecret: 4968a9430a32c10d969e395acd04117f',
 'X-Line-Trusted-User-With-ACL: uc6b4b81e65ce03b72a672ac34b6e3a60',
 'Content-type: application/json; charset=UTF-8',
 )
 );
 
 curl_setopt_array($curl, $options);
 $result = curl_exec($curl);
 curl_close($curl);
 return $result;
 } PHP
  37. 37. https://github.com/line/line-bot-sdk-php * composer required
  38. 38. FA C E B O O K M E S S E N G E R
  39. 39. • CNN • https://www.facebook.com/cnn/ • Humani: Jessie's Story • https://www.facebook.com/JessieHumani/
  40. 40. Your server Facebook M E S S E N G E R B O T Messages https:// https://messengerplatform.fb.com/
  41. 41. Your server Post Messages M E S S E N G E R B O T https://messengerplatform.fb.com/ Facebook
  42. 42. C R E AT I N G M E S S E N G E R B O T • 1. Create a Facebook App and Page • 2. Setup Webhook • 3. Get a Page Access Token • 4. Subscribe the App to the Page https://messengerplatform.fb.com/
  43. 43. YOUR_VERIFY_TOKEN <?php
 $verifyToken = @$_GET["hub_verify_token"];
 $challenge = @$_GET["hub_challenge"];
 
 if(strcmp($verifyToken, “YOUR_VERIFY_TOKEN”) === 0) {
 die($challenge);
 }
  44. 44. https://developers.facebook.com/docs/graph-api/webhooks
  45. 45. 4 . S U B S C R I B E T H E A P P T O T H E PA G E curl -X POST "https://graph.facebook.com/v2.6/me/ subscribed_apps?access_token=<PAGE_ACCESS_TOKEN>"
  46. 46. POST /fbbot/main.php HTTP/1.1 HOST: your.ip.here ACCEPT: */* ACCEPT-ENCODING: deflate, gzip CONTENT-TYPE: application/json X-HUB-SIGNATURE: sha1=03xxxxxxxxxxxxxxxxxxxxxxxxd58a7 CONTENT-LENGTH: 261 {"object":"page","entry":[{"id":1010700000009448,"time": 1460599788132,"messaging":[{"sender":{"id":1006290000095540},"recipient": {"id":1010710000019448},"timestamp":1460000088101,"message": {"mid":"mid.1460000088087:e7dca9a000001d9e91","seq":15,"text":"hi"}}]}]}
  47. 47. <?php
 $postdata = file_get_contents("php://input");
 
 $jsobj = json_decode($postdata, true);
 
 $results = $jsobj['entry'];
 
 if (isset($results)) {
 foreach ($results as &$entry) {
 foreach ($entry['messaging'] as &$msgObj) {
 $senderId = $msgObj['sender']['id'];
 $message = $msgObj['message']['text'];
 
 sendMessage($senderId, $message);
 }
 }
 }
 
 PHP
  48. 48. <?php
 function sendMessage($toId, $message)
 {
 $post = json_encode(array(
 "recipient" => array(
 "id" => $toId
 ),
 "message" => array(
 "text" => $message
 )
 )
 );
 
 $pageToken = "YOUR_PAGE_TOKEN";
 
 $ch = curl_init();
 $options = array(
 CURLOPT_URL => "https://graph.facebook.com/v2.6/me/messages?access_token=" . $pageToken,
 CURLOPT_HEADER => 0,
 CURLOPT_VERBOSE => 0,
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_POST => true,
 CURLOPT_POSTFIELDS => $post,
 CURLOPT_HTTPHEADER => array(
 'Content-type: application/json; charset=UTF-8'
 )
 );
 
 curl_setopt_array($ch, $options);
 $result = curl_exec($ch);
 curl_close($ch);
 return $result;
 } PHP
  49. 49. S K Y P E
  50. 50. Your server Skype S K Y P E B O T https://developer.microsoft.com/en-us/skype/bots/docs/api/chat Messages https://
  51. 51. Your server Skype Post Messages S K Y P E B O T https://developer.microsoft.com/en-us/skype/bots/docs/api/chat
  52. 52. S K Y P E B O T • 1. Create Skype bot • 2. Create Microsoft Application • 3. Paste Application ID to Skype bot • 4. Add your bot to contact https://developer.microsoft.com/en-us/skype/bots/docs/api/chat
  53. 53. T H E R E S O U R C E M O D E L Conversation ID • 8:<USERNAME> • 28:<BOTID> • 19:<GROUP CONVERSATION> • …. https://developer.microsoft.com/en-us/skype/bots/docs/api/chat
  54. 54. https://github.com/radutopala/skype-bot-php * composer required
  55. 55. https://developer.microsoft.com/en-us/skype/bots/docs/api/chat
  56. 56. POST /skypebot/main.php HTTP/1.1 CONTENT-TYPE: application/json; charset=utf-8 HOST: your.ip.here CONTENT-LENGTH: 156 EXPECT: 100-continue CONNECTION: Keep-Alive [{"id":"0","content":"Hi","activity":"message","from":"8:USERNAME","to":"28 :BOTID","time":"2016-06-16T07:08:03.906Z"}]
  57. 57. <?php
 $postdata = file_get_contents("php://input");
 
 $json = json_decode($postdata, true);
 
 foreach ($json as &$msgObj) {
 if (strcmp($msgObj['activity'], 'message') == 0) {
 $fromId = $msgObj['from'];
 $message = $msgObj['content'];
 sendMessage($fromId, $message);
 }
 }
 
 header("HTTP/1.1 201 Created"); PHP
  58. 58. https://developer.microsoft.com/en-us/skype/bots/docs/api/chat
  59. 59. <?php
 require __DIR__ . '/vendor/autoload.php';
 use SkypeClient;
 
 function sendMessage($id, $message)
 {
 $client = new Client([
 'clientId' => 'YOUR_CLIENT_ID',
 'clientSecret' => 'YOUR_CLIENT_SECRET',
 ]);
 $api = $client->authorize()->api('conversation');
 $result = $api->activity($id, $message);
 return $result;
 } PHP
  60. 60. Q & A http://www.britishacademyforonlinelearning.com/wp-content/uploads/2015/10/qanda.png
  61. 61. https://www.facebook.com/kobeengineer/posts/1777258705843732:0
  62. 62. https://www.facebook.com/groups/chatbot.tw/ Chatbot Developers Taiwan http://freeiconbox.com/icon/256/34429.png
  63. 63. ( )
  64. 64. https://www.facebook.com/groups/155659364581944/

×