-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlena.php
85 lines (72 loc) · 2.91 KB
/
lena.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
<?php
// including the library
require("lib/telegram.php");
// if already configured on config.php file, delete/comment following lines
$TELEGRAM_BOTNAME = "lenabot";
$TELEGRAM_TOKEN = "...";
$STATUS_ENABLE = false;
// basic configuration
$singletrigger = true; // if true, it tells the library to trigger at most a single callback function for each received message
// callbacks definition
function trigger_welcome($p) {
try {
$answer = "Welcome...";
$p->bot()->send_message($p->chatid(), $answer);
return logarray('text', $answer);
}
catch(Exception $e) { return false; } // you can also return what you prefer
}
function trigger_help($p) {
try {
$answer = "Try /lena to get a picture of Lena.";
$p->bot()->send_message($p->chatid(), $answer);
return logarray('text', $answer);
}
catch(Exception $e) { return false; }
}
function trigger_photo($p) {
try {
$pic = "lena.jpg";
$caption = "Look at Lena's picture!";
$p->bot()->send_photo($p->chatid(), "$pic", $caption);
return logarray('photo', "[$pic] $caption"); // you choose the format you prefer
}
catch(Exception $e) { return false; }
}
// callback to use if anything goes wrong
function trigger_err($p) {
if($p->chatid() < 0) { // if message has been sent from a member of a Telegram group
// ignore it and do not reply (to avoid not necessary messages on the group)
$response = logarray('ignore', null);
}
else {
// reply with an error message
$answer = "Error...";
$p->bot()->send_message($p->chatid(), $answer);
$response = logarray('error', $answer);
}
return $response;
}
// instantiating a new bot
$bot = new telegram_bot($TELEGRAM_TOKEN);
// receiving data sent from the user
$data = $bot->read_post_message();
$message = $data->message;
$date = $message->date;
$chatid = $message->chat->id;
$text = @$message->text;
// instantiating a new triggers set
$ts = new telegram_trigger_set($TELEGRAM_BOTNAME, $chatid, $singletrigger);
// registering the triggers
$ts->register_trigger_text_command("trigger_welcome", ["/start","/welcome","/hi"], 0); // state parameter is ignored
$ts->register_trigger_text_command("trigger_help", ["/help"], 0); // state parameter is ignored
$ts->register_trigger_text_command("trigger_photo", ["/lena","/getphoto","/photo","/picture"]); // state and count parameters are ignored
// error trigger
$ts->register_trigger_error("trigger_err"); // state parameter is ignored
// running triggers management
$response = $ts->run($bot, $message); // returns an array of triggered events
// log messages exchange on the database
@db_log($TELEGRAM_BOTNAME, 'recv', $chatid, 'text', $text, $date);
if(count($response)>0) foreach($response as $r) @db_log($TELEGRAM_BOTNAME, 'sent', $chatid, $r['type'], $r['content'], $date);
else @db_log($TELEGRAM_BOTNAME, 'error', $chatid, 'Error', '', $date);
?>