Skip to content

Commit 4992a66

Browse files
Update Packages System
1 parent 87385af commit 4992a66

23 files changed

+772
-67
lines changed

.htaccess

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
RewriteEngine On
2-
RewriteBase /
3-
4-
# Allow any files or directories that exist to be displayed directly
5-
RewriteCond %{REQUEST_FILENAME} !-f
62
RewriteCond %{REQUEST_FILENAME} !-d
7-
8-
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
3+
RewriteCond %{REQUEST_FILENAME} !-f
4+
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

composer.json

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
{
2-
"name": "paulao-dev/chat-bot-php-facebook",
3-
"description": "Chatbot para facebook feito em php OO",
4-
"type": "project",
5-
"authors": [
6-
{
7-
"name": "Chatbot Facebook PHP",
8-
"email": "jskhanframework@gmail.com"
9-
}
10-
],
112
"require": {
12-
"guzzlehttp/guzzle": "^6.2",
13-
"noahbuscher/macaw": "dev-master"
3+
"paulao-dev/router-khan": "*"
144
},
15-
"minimum-stability": "dev",
16-
"prefer-stable": true,
175
"autoload": {
18-
"psr-4": {
19-
"Src\\" : "src"
20-
}
6+
"psr-4": {
7+
"Src\\": "src/"
8+
}
219
}
2210
}

config/conexao/db.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
define("HOST", "HOST MYSQL");
4-
define("DB_NAME", "NAME DO BANCO");
5-
define("DB_USER", "USER");
6-
define("DB_PASS", "SENHA");
3+
define("HOST", "seu banco");
4+
define("DB_NAME", "neural");
5+
define("DB_USER", "user");
6+
define("DB_PASS", "senha");
77

88
$db = null;
99
try {

index.php

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,40 @@
33
require_once __DIR__ ."/vendor/autoload.php";
44
require_once 'config/botConfig/config.php';
55

6-
use \NoahBuscher\Macaw\Macaw as Route;
6+
use \App\RouterKhan\RouterKhan as Router;
7+
use \App\Container\ServiceContainer as Container;
78
use \Src\Bot\BotCore as BotCore;
89
use \Src\Bot\Callbacks as Callbacks;
910
use \Src\Bot\Facebook as Facebook;
1011

11-
Route::get('/debug', function(){
12+
$router = Router::getInstance();
13+
$container = Container::Build();
14+
$container->set('request', new \App\Request\Request());
15+
16+
$router->get('/', function($req, $res){
17+
echo "Ola Mundo!!";
18+
});
19+
20+
$router->get('/debug', function($req, $res){
1221
header('Content-Type: application/json');
1322
echo file_get_contents('neural/debug.json');
1423
});
1524

1625
// DEFINE AS ROTAS
1726

18-
Route::get('/server', function(){
27+
$router->get('/server', function($req, $res){
1928
include 'views/serversocket.html';
2029
});
2130

22-
Route::post('/server-websocket', function(){
31+
$router->post('/server-websocket', function($req, $res){
2332
file_put_contents('debug.txt', json_encode($_POST));
2433
$canal = strip_tags(addslashes($_POST['canal']));
2534
$data = json_encode($_POST['data']);
2635
$res = file_get_contents("https://gentle-ocean-75288.herokuapp.com/api/socket?canal=".$canal."&data=".urlencode($data));
2736
echo $res;
2837
});
2938

30-
Route::get('/webhook', function() {
39+
$router->get('/webhook', function($req, $res) {
3140

3241
// VERIFICAÇAO DO FACEBOOK
3342
$challenge = $_REQUEST['hub_challenge'];
@@ -36,47 +45,46 @@
3645
$token_access = "minhasenha123";
3746
// VERIFICACAO DE ACESSO A PARTIR DA SENHA
3847
if ($verify_token === $token_access) {
39-
echo $challenge;
40-
http_response_code(200);
48+
$res->send($challenge);
49+
$res->sendStatus(200);
4150
}else{
42-
die("Error");
43-
http_response_code(403);
51+
throw new Exception("Error Processing Webhook", 1);
52+
$res->sendStatus(403);
4453
}
4554

4655
});
4756

48-
Route::post("/webhook", function(){
57+
$router->post("/webhook", function($req, $res){
4958
// Cria o Robo
5059
$BotCore = BotCore::getInstance();
5160
$BotCore->logger(true);
52-
// Passa Callbacks junto com Api Rest do Facebook OO
53-
$BotCore->setCallbacks(new Callbacks(new Facebook(BOT_KEY)));
5461
// Bot Inicia
5562
$BotCore->Run();
5663
});
5764

5865
// Pega as rotas das Frases Para Callback
59-
Route::get("/neuros", function(){
66+
$router->get("/neuros", function($req, $res){
6067

6168
header("Content-Type: application/json");
6269
echo file_get_contents('neural/neuro-system.json');
6370

6471
});
6572

6673
// ROTA PARA TESTAR O ROBO FEITO
67-
Route::get('/teste/(:any)/(:any)', function($id, $msg){
68-
74+
$router->params('/teste/{id}/{msg}', function($req, $res) use($container){
75+
$request = $container->get('request');
76+
$id = $req->params('id');
77+
$msg = $req->params('msg');
6978
$callback = new Callbacks(new Facebook(BOT_KEY));
70-
print_r($callback->$msg(array("user_id" => $id)));
7179
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.BOT_KEY;
72-
$client = new \GuzzleHttp\Client(['headers' => [
73-
'Content-Type' => 'application/json'
74-
]]);
75-
$response = $client->post($url, array('body' => json_encode(array(
76-
"recipient" => array("id" => $id),
77-
"message" => $callback->$msg(array("user_id" => $id))
78-
))));
79-
80+
$response = $request->Post([
81+
"url" => $url,
82+
"header" => "Content-Type: application/json",
83+
"data" => json_encode([
84+
"recipient" => array("id" => $id),
85+
"message" => $callback->$msg(array("user_id" => $id))
86+
])
87+
]);
8088
});
8189

82-
Route::dispatch();
90+
$router->Run();

neural/debug.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"meu nome":"callbackName","oi":"callbackOi","boa noite":"callbackBoaNoite","fazer um robo igual":"callbackRoboIgual","\/clima":"callbackClima","\/youtube":"callbackYoutube","\/procurar":"callbackProcurar","ver logs":"callbackLogs","clicou_comecar":"callbackComecar","continua_conversa":"callbackContinuaConversa","help":"help"}

neural/facebook-cache.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"c4ca4238a0b923820dcc509a6f75849b":{"nome":" ","imagem":null,"localizacao":null,"sexo":null},"1d9b74aa8db0a836be9e583d1ab28ed7":{"nome":"Paulo Devs","imagem":"https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-1\/18447510_876074132543623_37819507484989563_n.jpg?oh=7102ebec6211bf77c37674e70cce317b&oe=59D0222E","localizacao":"pt_BR","sexo":"male"},"a885bf5ac61516cd4cc7ebdd6a74f6ea":{"nome":"Willian Emboava","imagem":"https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-1\/12932682_1740979119452194_4886200062784641136_n.jpg?oh=a27d697dc1ca40988fb5bb7039e1c702&oe=59DCC192","localizacao":"pt_BR","sexo":"male"},"28a1ec14abc28219b0245d53b226aeef":{"nome":"Denilson Barbosa da Silva","imagem":"https:\/\/scontent.xx.fbcdn.net\/v\/t31.0-1\/15800416_1332418220164393_6602546720838651165_o.jpg?oh=d1492a40c822d6f75210de918390becb&oe=59C753CB","localizacao":"pt_BR","sexo":"male"},"5b3d6d79c9c01878b59340566bbf9c7e":{"nome":"J\u00fanior Gon\u00e7alves","imagem":"https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-1\/19029502_10209608619432968_4849056386510388126_n.jpg?oh=9c495709d31be09378466fbd3cac2824&oe=59E1A0EF","localizacao":"pt_BR","sexo":"male"},"3662036853669c7a4c55c71a7defaa66":{"nome":"Luiz Andrerson Lourencini","imagem":"https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-1\/14238286_1087598794680542_522981282253665541_n.jpg?oh=e5d1fb7b7725e1dabaa2efbe9ede6604&oe=59D530B4","localizacao":"pt_BR","sexo":"male"},"308200c84ce97cdacbdb93092e4c178a":{"nome":"Rodrigo Rodrigo","imagem":"https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-1\/12144659_906855612741690_5376167678356907324_n.jpg?oh=22db98008c6e8092807f694f6b0d4be6&oe=59DE3ED3","localizacao":"es_ES","sexo":"male"},"f6c81e1742f995f33538d297ea4b80aa":{"nome":"Enio Ribeiro","imagem":"https:\/\/scontent.xx.fbcdn.net\/v\/t31.0-1\/18768219_1204620899665403_8710454326243704550_o.jpg?oh=1da8249f2ba7e2472ac21c1b61d83dee&oe=59D678C8","localizacao":"pt_BR","sexo":"male"},"17f0654338f584cb09ec354b32cbca78":{"nome":"Neylton Benjamim","imagem":"https:\/\/scontent.xx.fbcdn.net\/v\/t31.0-1\/14424734_1763240320599615_2857761303547464381_o.jpg?oh=5cab99bef01df1954b9d35db572f13e2&oe=5A0A3328","localizacao":"pt_BR","sexo":"male"},"32cf9310948d4b05390bf412de5e1cb2":{"nome":"Eric Magalh\u00e3es","imagem":"https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-1\/12313938_123793014657085_1670448537007722927_n.jpg?oh=b6668a46fc0795c1bd06294e75b39495&oe=59C32C75","localizacao":"pt_BR","sexo":null},"779219baaa3c78debe7ae6a17404dafb":{"nome":"Jonhy Bravo","imagem":"https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-1\/316869_111433252305128_1125988955_n.jpg?oh=afe7da3149924096f6e4a011a30561bf&oe=5A10FAC8","localizacao":"pt_BR","sexo":null},"1b67da0f1079bede4f52110a789aeb45":{"nome":"Marcio Soethe","imagem":"https:\/\/scontent.xx.fbcdn.net\/v\/t31.0-1\/15325369_1267694023304310_7041893711383586416_o.jpg?oh=7cd234d833cdccfc73024af6cb9a4dbd&oe=59DA9E0A","localizacao":"pt_BR","sexo":null},"14d7cfef6b3f46513172f43263ccbbe9":{"nome":"Alexandre Matos","imagem":"https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-1\/11262426_418697241633607_2420963974696030714_n.jpg?oh=c357741dce14ac36e7d90c340368c78b&oe=59CE3F67","localizacao":"pt_BR","sexo":null},"99d5c4b4da20218bb8b5c9a924a37d0c":{"nome":"Leonardo Nascimento","imagem":"https:\/\/scontent.xx.fbcdn.net\/v\/t31.0-1\/16601757_1335365189855558_5953969015414086874_o.jpg?oh=9d33b51afc312baad625c52b42dd13a4&oe=59C7EBB1","localizacao":"pt_BR","sexo":null},"87cac396acd09fae556cfeab599406a7":{"nome":"Maycon Oliveira","imagem":"https:\/\/scontent.xx.fbcdn.net\/v\/t1.0-1\/10253809_226463827543081_5483567575554799908_n.jpg?oh=c31911fbb48c42736299fa1189947aee&oe=59C54720","localizacao":"pt_BR","sexo":"male"},"d203c3dceac6b0d41b0c31073e1df34a":{"nome":"Diego Jos\u00e9 Soek","imagem":"https:\/\/scontent.xx.fbcdn.net\/v\/t31.0-1\/p960x960\/244013_102396466520672_1794629_o.jpg?oh=26cd5d75be9abd60f6e4459a2635b805&oe=5A05FE25","localizacao":"pt_BR","sexo":"male"},"4d0e89cb7e9fe576084377b9e2e9750d":{"nome":"Luciano Lopez","imagem":"https:\/\/scontent.xx.fbcdn.net\/v\/t31.0-1\/19025198_1243329125795685_3800916630895114026_o.jpg?oh=183e9df0a0bf6784eda214e82b9dc635&oe=5A0BE994","localizacao":"pt_BR","sexo":"male"},"303752b82ce1e9e35df1000215fd4e13":{"nome":"Ali Nabeel Ahmed","imagem":"https:\/\/scontent.xx.fbcdn.net\/v\/t31.0-1\/19417407_10156289029102564_3792450333365943262_o.jpg?oh=927c507d7c708a667db1b7a259689497&oe=59C917A7","localizacao":"en_US","sexo":"male"}}

src/Bot/BotCore.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

33
namespace Src\Bot;
4+
use Src\Bot\Callbacks as Callbacks;
5+
use Src\Bot\Facebook as Facebook;
46

57
require_once 'config/botConfig/config.php';
68

@@ -28,6 +30,7 @@ protected function __construct(){
2830
self::$token = BOT_TOKEN;
2931
self::$endpoint = BOT_ENDPOINT;
3032
self::$dominio = BOT_DOMINIO;
33+
self::$callbacks = new Callbacks(new Facebook(BOT_KEY));
3134
}
3235

3336
// Configs do ChatBot
@@ -41,33 +44,27 @@ public function logger($cond){
4144
}
4245
}
4346

44-
public function setCallbacks($callback){
45-
self::$callbacks = $callback;
46-
}
47-
4847
// Envia o Log Pelo Pusher
4948
public function MsgPusher($msg){
50-
$canal = "chatbotphp";
49+
/*$canal = "chatbotphp";
5150
$post = new \Src\Http\Post("http://chatbotphp.ga/server-websocket", array(
5251
"canal" => $canal,
5352
"data" => $msg
54-
));
53+
));*/
5554
}
5655

5756
public function sendApi($d){
5857
/* KEY DA PAGINA GERADO NO MESSENGER NO FACEBOOK DEVELOPERS */
5958
$key = self::$key;
59+
$request = new \App\Request\Request();
6060
// Rest do Chatbot
6161
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.$key;
6262
// Iniciando o Envio.
63-
$client = new \GuzzleHttp\Client(['headers' => [
64-
'Content-Type' => 'application/json'
65-
]]);
66-
//file_put_contents('neural/debug.json', json_encode($d));
67-
// Envia Requisicao
68-
if (!empty($d['message'])){
69-
$client->post($url, array('body' => json_encode($d)));
70-
}
63+
$request->Post([
64+
"url" => $url,
65+
"header" => "Content-Type: application/json",
66+
"data" => json_encode($d)
67+
]);
7168
}
7269

7370
public function eventsTrigger($id, $text, $user){

src/Bot/Callback.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
namespace Src\Bot;
4+
5+
class Callbacks {
6+
7+
public function __construct(){
8+
return $this;
9+
}
10+
11+
public function montaBotao($id, $txt, $btns){
12+
13+
return array(
14+
"attachment" => array(
15+
"type" => "template",
16+
"payload" => array(
17+
"template_type" => "button",
18+
"text" => $txt,
19+
"buttons" => $btns
20+
)));
21+
22+
}
23+
24+
public function callbackName($info){
25+
26+
$getPessoa = Facebook::get($info["user_id"]);
27+
return array("text" => "Seu Nome é: ".$getPessoa["nome"]);
28+
29+
}
30+
31+
public function callbackOi($info){
32+
33+
$getPessoa = Facebook::get($info["user_id"]);
34+
return array("text" => "Olá, tudo bem ".$getPessoa["nome"]." ?");
35+
36+
}
37+
38+
public function callbackBoaNoite($info){
39+
40+
$pessoa = Facebook::get($info["user_id"]);
41+
$n = explode(" ", $pessoa["nome"]);
42+
return array("text" => "Olá ".$n[0].", Boa Noite!!");
43+
44+
}
45+
46+
public function callbackRoboIgual(){
47+
48+
return array("text" => "https://github.com/PaulaoDeveloper/ChatBot-Messenger-PHP");
49+
50+
}
51+
52+
public function callbackClima($res){
53+
54+
55+
$resValue = explode(" ", $res["extern_value"]);
56+
$clima = (array) json_decode(file_get_contents("http://chatbotphp.ga/rest?cidade={$resValue[0]}&estado={$resValue[1]}"));
57+
$fraseClima = "{$clima["temperatura"]}ºC \n{$clima["descricao"]} \n🕐 {$clima["periodo"]} \n🎈 Umidade: {$clima["humidade"]} \n🌀 {$clima["v_vento"]} \n📅 {$clima["dia"]} \n🕒 {$clima["horario"]}";
58+
//.' ºC'
59+
return array("text" => $fraseClima);
60+
61+
}
62+
63+
public function callbackProcurar($user){
64+
65+
$s = urlencode($user["extern_value"]);
66+
$response = (array) json_decode(file_get_contents("https://pt.wikipedia.org/w/api.php?action=query&list=search&origin=*&srsearch={$s}&format=json"));
67+
$res = (array) $response["query"];
68+
$res = (array) $res["search"][0];
69+
$res['snippet'] = trim(strip_tags($res['snippet']));
70+
return array("text" => "{$res['title']}: \n{$res['snippet']}");
71+
72+
}
73+
74+
public function callbackYoutube($user){
75+
76+
$video = urlencode($user["extern_value"]);
77+
$response = (array) json_decode(file_get_contents("https://www.googleapis.com/youtube/v3/search?part=snippet,id&type=video&q={$video}&key=AIzaSyCNhqVjoxDfgX7WlNDvQaf3PLHQI8uxFwk"));
78+
$res = (array) $response["items"][0];
79+
$res = (array) $res["id"];
80+
$res = $res["videoId"];
81+
82+
if(!empty($res)){
83+
return array("text" => "https://youtu.be/".$res);
84+
}else{
85+
return array("text" => "Nenhum video foi Encontrado!!");
86+
}
87+
88+
}
89+
90+
public function callbackLogs(){
91+
92+
return array("text" => "https://".$_SERVER['HTTP_HOST']."/logs");
93+
94+
}
95+
96+
public function callbackComecar($info){
97+
98+
$estado[$info["sender"]["id"]] = "comecando";
99+
$dataBtn = $this->montaBotao($info["sender"]["id"], "Escolha uma Opção", array(
100+
array(
101+
"type" => "web_url",
102+
"url" => "https://github.com/PaulaoDeveloper/ChatBot-Messenger-PHP",
103+
"title"=> "Repositorio"
104+
),
105+
array(
106+
"type" => "postback",
107+
"title" => "Continuar Conversa",
108+
"payload" => "continua_conversa"
109+
)
110+
));
111+
112+
//$pessoa = getFacebookPessoa($info["sender"]["id"]);
113+
//$n = explode(" ", $pessoa["nome"]);
114+
return $dataBtn;
115+
116+
}
117+
118+
public function callbackContinuaConversa($info){
119+
120+
$id = $info["sender"]["id"];
121+
return array("text" => "\n Estado: ".json_encode($estado));
122+
//callbackOi(array("user_id" => $id));
123+
124+
}
125+
126+
public function help(){
127+
128+
return array("text" => "↪ Meu Nome \n↪ Oi \n↪ Boa Noite \n↪ Fazer um robo igual \n↪ /clima 'cidade' 'estado em sigla' \n↪ /procurar 'algo para pesquisar' \n↪ /youtube 'Procurar Video No Youtube'");
129+
130+
}
131+
132+
}

src/Bot/Callbacks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function callbackContinuaConversa($info){
153153

154154
public function help(){
155155

156-
return array("text" => "↪ Meu Nome \n↪ Oi \n↪ Boa Noite \n↪ Fazer um robo igual \n↪ /clima 'cidade' 'estado em sigla' \n↪ /procurar 'algo para pesquisar' \n↪ /youtube 'Procurar Video No Youtube'");
156+
return array("text" => "↪ Meu Nome \n↪ Oi \n↪ Boa Noite \n↪ Fazer um robo igual \n↪ /clima 'cidade'-'estado em sigla' \n↪ /procurar 'algo para pesquisar' \n↪ /youtube 'Procurar Video No Youtube'");
157157

158158
}
159159

0 commit comments

Comments
 (0)