Skip to content

Commit 03666e9

Browse files
committed
First init
1 parent 62d4e5d commit 03666e9

File tree

4 files changed

+111
-2
lines changed

4 files changed

+111
-2
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor

Diff for: README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1-
# slack-bot-with-simsimi
2-
Example Slack bot in PHP language that connect to Simsimi api sandbox.
1+
# PHP Slack Bot with Simsimi api.
2+
Example Slack bot in PHP language that connect to Simsimi api sandbox. This one support simsimi trial api. If you want to use full simsimi api, you must change endpoint config to endpoint of paid api. You can make bot with stupid talking style. LOL
3+
4+
# How to use
5+
- Get slack token for bot can access slack api. Get your token here https://my.slack.com/services/new/bot.
6+
- Get sandbox token from simsimi. Get your token here http://developer.simsimi.com.
7+
- Open bot.php and add these token to config.
8+
- Run `composer install` to install dependency packages.
9+
- Run `php bot.php` to start bot.
10+
- Invite your bot to channel and try to mention it. It will talk back to you. Have fun.

Diff for: bot.php

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

Diff for: composer.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"minimum-stability" : "dev",
3+
"require": {
4+
"jclg/php-slack-bot": "dev-master"
5+
}
6+
}

0 commit comments

Comments
 (0)