-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.php
77 lines (59 loc) · 1.65 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
<?php
define("MIN_QUERY_LENGTH", 2);
define("MAX_QUERY_LENGTH", 50);
header('Content-Type: application/json');
/*
Input format:
{
"update_id": 440148654,
"inline_query": {
"id": "817150061370106850",
"from": {
"id": 190257574,
"is_bot": false,
"first_name": "Kolodi",
"username": "kolodim",
"language_code": "en-US"
},
"query": "ally",
"offset": ""
}
}
*/
$content = file_get_contents("php://input");
$update = json_decode($content, true);
if (!$update)
{
exit("no input");
}
$q = $update['inline_query']['query'];
$queryLenght = strlen($q);
if ($queryLenght < MIN_QUERY_LENGTH) {
Exit("query too short");
}
if ($queryLenght > MAX_QUERY_LENGTH) {
Exit("query too long");
}
include "lib.php";
include "db.php";
include "process_query.php";
$db = new DB();
// put user in db
$user = $update['inline_query']['from'];
$user["is_bot"] = intval(boolval($user["is_bot"]));
$sql = "INSERT INTO tg_user (id, is_bot, first_name, username, language_code) VALUES (?,?,?,?,?)";
$stmt = $db->pdo->prepare($sql);
$stmt->execute(array_values($user));
// put query in db
$inline_query = $update["inline_query"];
$inline_query["from"] = $user["id"];
$sql = "INSERT INTO inline_query (id, user, query, offset) VALUES (?,?,?,?)";
$stmt = $db->pdo->prepare($sql);
$stmt->execute(array_values($inline_query));
$inlineQueryId = $update['inline_query']['id'];
$tg = new TG([your_bot_id_here]);
$responce = GetInlineAnswer($q, $db);
$responce->inline_query_id = $inlineQueryId;
//echo $responce->inline_query_id;
$data_string = json_encode($responce);
$result = $tg->SendInlineAnswerToTG($data_string);