-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.php
94 lines (73 loc) · 2.66 KB
/
bot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
require 'vendor/autoload.php';
use PhpSlackBot\Bot;
$config = [
'slack' => [
'token' => 'xoxb-xxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxx'
],
'simsimi' => [
// 'endpoint' => 'http://api.simsimi.com/request.p', // paid key
'endpoint' => 'http://sandbox.api.simsimi.com/request.p', // trial key
'token' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'locale' => 'en' // View locale support at http://developer.simsimi.com/lclist.
]
];
function curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$html = curl_exec($ch);
curl_close($ch);
return $html;
}
function talkToSimsimi($text) {
global $config;
$json = curl($config['simsimi']['endpoint']
."?key=".$config['simsimi']['token']
."&lc=".$config['simsimi']['locale']
."&ft=1.0&text=".urlencode($text));
$arr = json_decode($json, true);
if(empty($arr['response'])) {
// This trial api will have less db. Use paid key for full db. I don't try so I don't know it worth or not?
$arr['response'] = "[Simsimi not response.]";
}
return $arr['response'];
}
class SimsimiCommand extends \PhpSlackBot\Command\BaseCommand {
protected function configure() {
// We don't have to configure a command name in this case
}
protected function execute($data, $context)
{
$botID = $context['self']['id'];
// if user is bot, skip it.
if (!empty($data['user']) && $data['user'] == $botID) {
return;
}
if (empty($data["channel"])) {
return;
}
$text = null;
if (!empty($data['type']) && $data['type'] == 'message' && !empty($data['text'])) {
$text = $data['text'];
}
if (!empty($data['comment'])) {
$text = $data['comment'];
}
if (!empty($text)) {
// check bot got mention?
if (strpos($text, '<@'.$botID.'>') === 0) {
$text = preg_replace('#<\@'.$botID.'>:? ?#', '', $text); // remove bot name
$text = preg_replace('#<\@[A-Z0-9]+>#', '', $text); // remove slack user id mention out. Simsimi don't understand it.
$this->send($data["channel"], $data['user'], talkToSimsimi($text));
return;
}
}
}
}
$bot = new Bot();
$bot->setToken($config['slack']['token']); // Get your token here https://my.slack.com/services/new/bot
$bot->loadCatchAllCommand(new SimsimiCommand());
$bot->run();