-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
148 lines (130 loc) · 5.79 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* @author EtMDB Devs ([email protected])
* @copyright 15/08/2017
* @license https://www.etmdb.com/license
* @version 1.1.0
*/
require_once(__DIR__ . '/init.php');
use EtMDB\TelegramBot\RequestHandler;
use EtMDB\TelegramBot\SystemLog;
use EtMDB\TelegramBot\DataHandler;
use EtMDB\TelegramBot\ApiAccessManager;
define('SEARCH_MOVIE', 0);
define('SEARCH_PEOPLE', 1);
define('SEARCH_CINEMA', 2);
define('SEARCH_COMPANY', 3);
SystemLog::addLog('-------------------------------------');
SystemLog::addLog('-------------------------------------');
SystemLog::addLog('-------------------------------------');
SystemLog::addLog('-------------------------------------');
SystemLog::addLog('Start process ....');
$requestHandler = new RequestHandler($config);
$apiAccessHandler = new ApiAccessManager($config);
// check if anything is received
$jsonFromTG = file_get_contents("php://input");
if (isset($jsonFromTG)) {
$decodedJsonFromTG = DataHandler::convertToJson($jsonFromTG);
// if received a PM
if (isset($decodedJsonFromTG["message"]["chat"]["id"])) {
$sessionChatID = $decodedJsonFromTG["message"]["chat"]["id"];
$messageText = $decodedJsonFromTG['message']['text'];
// This if statement works on all /commands sent to the bot
// This is made to prevent user confusion
if ($messageText[0] == "/") {
$keyboard = array(
"inline_keyboard" => array(
array(
array(
"text" => "Search movies",
"switch_inline_query_current_chat" => ""
)
),
array(
array(
"text" => "Search artists (/a)",
"switch_inline_query_current_chat" => "/a "
)
),
array(
array(
"text" => "Search cinemas (/b)",
"switch_inline_query_current_chat" => "/b "
)
),
array(
array(
"text" => "Search film companies (/c)",
"switch_inline_query_current_chat" => "/c "
)
)
)
);
$keyboard = json_encode($keyboard, true);
$requestHandler->sendTextMessage($sessionChatID, "What would you like to search for?", "HTML", $keyboard);
} else if (isset($decodedJsonFromTG['message']['entities'])) {
//This works on messages that contain entities, such as the article the bot receives when an inline result is chosen
} else {
$keyboard = array(
"inline_keyboard" => array(
array(
array(
"text" => "Search movies",
"switch_inline_query_current_chat" => $messageText
)
),
array(
array(
"text" => "Search artists (/a)",
"switch_inline_query_current_chat" => "/a " . $messageText
)
),
array(
array(
"text" => "Search cinemas (/b)",
"switch_inline_query_current_chat" => "/b " . $messageText
)
),
array(
array(
"text" => "Search film companies (/c)",
"switch_inline_query_current_chat" => "/c " . $messageText
)
)
)
);
$keyboard = json_encode($keyboard, true);
$requestHandler->sendTextMessage($sessionChatID, "<i>Where should I search for: </i><b>" . $messageText . "</b>", "HTML", $keyboard);
}
} // if received an inline query
else
if (isset($decodedJsonFromTG["inline_query"]["id"])) {
$sessionQueryID = $decodedJsonFromTG["inline_query"]["id"];
$queryText = $decodedJsonFromTG["inline_query"]["query"];
$searchQueryTerm = $queryText;
$inlineResults = array();
// Split command and query
if ($queryText[0] == '/' && $queryText[1] != ' ') {
$searchQueryTerm = substr($queryText, 3);
if ($queryText[1] == 'a') {
// search in people
$jsonFromETMDB = $requestHandler->search($searchQueryTerm, SEARCH_PEOPLE);
$inlineResults = $requestHandler->getInlinePeopleResult($jsonFromETMDB);
} else
if ($queryText[1] == 'b') {
// search in cinemas
$jsonFromETMDB = $requestHandler->search($searchQueryTerm, SEARCH_CINEMA);
$inlineResults = $requestHandler->getInlineCinemasResult($jsonFromETMDB);
} else
if ($queryText[1] == 'c') {
// search film companies
$jsonFromETMDB = $requestHandler->search($searchQueryTerm, SEARCH_COMPANY);
$inlineResults = $requestHandler->getInlineCompaniesResult($jsonFromETMDB);
}
} else {
$jsonFromETMDB = $requestHandler->search($searchQueryTerm, SEARCH_MOVIE);
$inlineResults = $requestHandler->getInlineMoviesResult($jsonFromETMDB);
}
$requestHandler->showInlineResults($sessionQueryID, $inlineResults);
}
}