-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Valentinas Bartusevičius
committed
Nov 21, 2017
0 parents
commit e594d73
Showing
7 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
vendor/ | ||
composer.lock | ||
ngrok* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
## Paysera Chat Bot Workshop | ||
|
||
### Requirements | ||
* `PHP >=5.5` | ||
* [ngrok](https://ngrok.com/download) | ||
* Facebook Account | ||
|
||
### Setup instructions | ||
|
||
#### PHP server | ||
For simplicity of this task, we will use built-in PHP server. | ||
* from project's directory run in console `php -S localhost:{random_port}` please use any number between 1024 and 65535 for `{random_port}`. | ||
* check local PHP server is running - in browser go to `http://localhost:{random_port}/` - you should see a blank page. | ||
|
||
#### ngrok | ||
Ngrok is local proxy allowing your PC to be accessible from Internet. | ||
* download `ngrok` from https://ngrok.com/download and extract it in project directory. | ||
* make sure it is executable - run in console: `./ngrok`. | ||
* start `ngrok` to have public proxy - `./ngrok http {random_port}` - here `{random_port}` is from step above. | ||
* make sure everything is OK with `ngrok` by going to [http://127.0.0.1:4040/inspect/http](http://127.0.0.1:4040/inspect/http) | ||
|
||
#### Facebook App Setup | ||
|
||
1. Go to [Messenger API platform](https://developers.facebook.com/docs/messenger-platform) and click `Try It now` | ||
![](doc/1.png) | ||
|
||
1. Launch Test Drive: ![](doc/2.png) | ||
1. Fill required steps: | ||
1. Check the checkbox | ||
1. Skip Node.JS installation | ||
1. Enter chat bot name - must start with capital letter | ||
1. Skip package download - we do not need it | ||
1. Fill url you got from `ngrok` setup - do not press `Next`, you will get an error ![](doc/3.png) | ||
1. In another browser tab go to [Facebook Apps](https://developers.facebook.com/apps) | ||
1. Click on App with your chat bot name (You filled it in `Step 3`) | ||
1. Save the `App ID` and `App Secret` - in configuration file | ||
1. Go to `Messenger` in `Products` sidebar | ||
1. Under `Token Generation` select `Page` with same name as your chat bot name, save the `Page Access Token` in configuration | ||
1. Go to previous browser tab and click `Next` in `Step 5` | ||
1. If everything was according to plan, you can open Messenger, search for chat bot and he should reply to you. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "paysera/app-chat-bot-workshop", | ||
"description": "A simple chat bot Workshop", | ||
"type": "project", | ||
"scripts": { | ||
"post-install-cmd": [ | ||
"@downloadNgrok" | ||
], | ||
"post-update-cmd": [ | ||
"@downloadNgrok" | ||
], | ||
"downloadNgrok": [ | ||
"wget -q https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip -O t.z; unzip -o t.z; rm t.z" | ||
] | ||
}, | ||
"require": { | ||
"facebook/graph-sdk": "^5.6" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
include (__DIR__ . '/vendor/autoload.php'); | ||
|
||
$access_token = 'EAAE2Y3KjNyYBAHpKjqSZAmGN8YvpkC09Duh1deYrqV4ZC2NrOpPgbo9b4njEZCJOkusX8Yw1b6JiXX4ukvXIZBtn5ZCZBZBbfEa6VQV46p6P8O0MZAjNUIgNZAZAUrHOfVCqRPdO5v7ECtCZCok5es60GPmyVNHEUfbjZCldFmxk9qsNp9kfa8ZCTG45j'; | ||
$verify_token = 'TOKEN'; | ||
$appId = '341275729671974'; | ||
$appSecret = 'f7fbe7249cdffb4563e6635682a2eee2'; | ||
|
||
if(isset($_REQUEST['hub_challenge'])) { | ||
$challenge = $_REQUEST['hub_challenge']; | ||
if ($_REQUEST['hub_verify_token'] === $verify_token) { | ||
echo $challenge; die(); | ||
} | ||
} | ||
|
||
$input = json_decode(file_get_contents('php://input'), true); | ||
$message = $input['entry'][0]['messaging'][0]['message']['text']; | ||
$sender = $input['entry'][0]['messaging'][0]['sender']['id']; | ||
|
||
$fb = new \Facebook\Facebook([ | ||
'app_id' => $appId, | ||
'app_secret' => $appSecret, | ||
]); | ||
|
||
$data = [ | ||
'recipient' => [ | ||
'id' => $sender, | ||
], | ||
'message' => [ | ||
'text' => 'You wrote: ' . $message, | ||
] | ||
]; | ||
|
||
$response = $fb->post('/me/messages', $data, $access_token); | ||
|