forked from piterskiy/etuts-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_state.php
81 lines (70 loc) · 1.87 KB
/
handle_state.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
<?php
//--------------------- Enum of STATEs ---------------------------
define("IDLE", 0);
define("CONTACT", 1);
define("CONTACT_ADMIN_ANSWER", 2);
define("POST_VALIDATION_SEND_POST_TITLE", 3);
define("MOAREFI_ROBOT_BOT_ID", 4);
define("MOAREFI_ROBOT_BOT_TITLE", 5);
define("MOAREFI_ROBOT_BOT_IMAGE", 6);
define("MOAREFI_ROBOT_CAPTION", 7);
define("MOAREFI_ROBOT_SCHEDULE_POST", 8);
define("REQUEST_POST",9);
define("POST_SOURCE",10);
define("AUTHOR_VALIDATION", 11);
// get chat state from database
function get_chat_state($text, $username, $fullname) {
global $db;
$state = IDLE; // no state
if ($db->user_is_new()) {
log_debug($db->insert($state, $text, $username, $fullname));
} else {
$state = $db->get_state();
$db->set_last_message($text);
$db->set_username($username);
$db->set_fullname($fullname);
}
return $state;
}
function handle_state($state, $chat_id, $text, $message_id, $message) {
$func = $class = '';
switch ($state) {
case CONTACT:
case CONTACT_ADMIN_ANSWER:
$class = 'contact_command';
break;
case POST_VALIDATION_SEND_POST_TITLE:
$class = 'post_validation_command';
break;
case MOAREFI_ROBOT_BOT_ID:
case MOAREFI_ROBOT_BOT_TITLE:
case MOAREFI_ROBOT_BOT_IMAGE:
case MOAREFI_ROBOT_CAPTION:
case MOAREFI_ROBOT_SCHEDULE_POST:
$func = 'btn_moarefi_robot';
break;
case REQUEST_POST:
$class = 'request_post_command';
break;
case POST_SOURCE:
$class = 'post_source_command';
break;
case AUTHOR_VALIDATION:
$class = 'aut_val_command';
break;
case IDLE:
default:
return false;
}
if (class_exists($class)) {
$command = new $class();
$command->run($chat_id, $text, $message_id, $message, $state);
} else if ($func !== '') {
$func($chat_id, $text, $message_id, $message, $state);
}
return true;
}
function add_admin($admin_chat_id) {
global $db;
$db->set_permission(ADMIN, $admin_chat_id);
}