-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.php
208 lines (171 loc) · 6.83 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
require ("config.inc.php");
// Inline Query handling
// When no or an unknown string (= language code) is given, generate a completely random (language + insult) one
if ($query != null && $query != '' || $query == '')
{
if( array_key_exists($query, $supportedLanguages) )
{
$generatedInsult = generateInsult($query);
$results = json_encode([['type' => 'article', 'id' => '1', 'title' => $supportedLanguages[$query].' Insult', 'message_text' => $generatedInsult]]);
$content = ['inline_query_id' => $queryid, 'results' => $results, 'cache_time' => 1, 'is_personal' => 'true', 'next_offset' => '', ];
$reply = $telegram->answerInlineQuery($content);
}
else
{
$generatedInsult = generateInsult('en');
switch ($query)
{
case '':
$rand_lang = random_key($supportedLanguages);
$generatedInsult = generateInsult($rand_lang);
$results = json_encode([['type' => 'article', 'id' => '1', 'title' => 'Random Insult', 'message_text' => $generatedInsult], ['type' => 'article', 'id' => '2', 'title' => 'Send Homepage', 'message_text' => forURL() , 'parse_mode' => 'HTML']]);
$content = ['inline_query_id' => $queryid, 'results' => $results, 'cache_time' => 1, 'is_personal' => 'true', 'next_offset' => '', ];
$reply = $telegram->answerInlineQuery($content);
break;
default:
$rand_lang = random_key($supportedLanguages);
$generatedInsult = generateInsult($rand_lang);
$results = json_encode([['type' => 'article', 'id' => '1', 'title' => $generatedInsult, 'message_text' => $generatedInsult]]);
$content = ['inline_query_id' => $queryid, 'results' => $results, 'cache_time' => 1, 'is_personal' => 'true', 'next_offset' => '', ];
$reply = $telegram->answerInlineQuery($content);
break;
}
}
}
if ($text == '/start')
{
// Create a permanent custom keyboard
$option = array(
// First row
array(
$telegram->buildKeyboardButton("Generate Insult")
) ,
// Second row
array(
$telegram->buildKeyboardButton("Change Language") ,
$telegram->buildKeyboardButton("Website") ,
$telegram->buildKeyboardButton("Help")
)
);
$keyb = $telegram->buildKeyBoard($option, $onetime = false, $resize = true);
// First response after receiving "/start" from userLang
$content = array(
'chat_id' => $chat_id,
'reply_markup' => $keyb,
'text' => "
Hi, this is the official Evil Insult Generator Telegram Bot! \xf0\x9f\x98\x88 \n
Evil Insult Generator's goal is to offer the most evil insults. Please help us to reach this honorable purpose by submitting insults via mail. \n
The button Generate Insult sends you a random insult in the language you selected with the Change Language button. Default is English. \n
Clicking on Homepage gives you the link to our awesome homepage. \n
The bot supports Telegram groups and inline messages. \n
@EvilInsultGeneratorBot without any additional info shows the menu where you can send a random insult or the link to the homepage.\n
If you add a language code (e.g. @EvilInsultGeneratorBot la) it shows you a preview in that language and you can click on it to submit it.\n
In case you write an unsupported language code (e.g. @EvilInsultGeneratorBot xyz) it sends one completely random insult. \n
Questions and feedback are very welcome. \n
Take care!
"
);
$telegram->sendMessage($content);
}
switch ($text)
{
// Website Button
case 'Website':
$content = ['chat_id' => $chat_id, 'text' => forURL() , 'parse_mode' => 'HTML'];
$telegram->sendMessage($content);
break;
// Help Button
case 'Help':
$content = ['chat_id' => $chat_id, 'text' => getHelp() ];
$telegram->sendMessage($content);
break;
// Generate Insult Button
case 'Generate Insult':
$stmtSELECT->execute(array(
'chatid' => $chat_id
));
$result = $stmtSELECT->fetchAll();
$userLang = $result[0]['language'];
$generatedInsult = generateInsult($userLang);
$content = ['chat_id' => $chat_id, 'text' => $generatedInsult];
$telegram->sendMessage($content);
break;
// Change Language Button, only German and English are enabled for now
// $telegram->buildInlineKeyBoardButton('Spanish', $url = '', $callback_data = 'es') ,
case 'Change Language':
$option = [[$telegram->buildInlineKeyBoardButton('English', $url = '', $callback_data = 'en') , $telegram->buildInlineKeyBoardButton('German', $url = '', $callback_data = 'de') , ], ];
$keyb = $telegram->buildInlineKeyBoard($option);
$content = ['chat_id' => $chat_id, 'reply_markup' => $keyb, 'text' => 'Please select a language'];
$telegram->sendMessage($content);
break;
default:
break;
}
// Get language Callback
$callback_query = $telegram->Callback_Query();
if ($callback_query !== null && $callback_query != '')
{
$stmtSELECT->execute(array(
'chatid' => $chat_id
));
$result = $stmtSELECT->fetchAll();
if (count($result) > 0)
{
// User already exists
$newlanguage = $telegram->Callback_Data();
$reply = 'Language switched to: ' . $newlanguage;
$content = ['chat_id' => $telegram->Callback_ChatID() , 'text' => $reply];
$telegram->sendMessage($content);
$stmtUPDATE->execute(array(
'chatid' => $chat_id,
'language' => $newlanguage
));
}
else
{
// User is not known
$newlanguage = $telegram->Callback_Data();
$content = ['chat_id' => $chat_id, 'text' => 'Language has been set!'];
$telegram->sendMessage($content);
$stmtINSERT->execute(array(
'chatid' => $chat_id,
'language' => $newlanguage
));
}
}
// Backend call to generate insult
function generateInsult($lang)
{
$insult = file_get_contents("https://evilinsult.com/generate_insult.php?lang=" . $lang);
$insult = htmlspecialchars_decode($insult);
return $insult;
}
// Get random key from an array
function random_key($array){
$keys=array_keys($array);
return $keys[mt_rand(0, count($keys) - 1)];
}
// Format Website URL as HTML message
function forURL()
{
$url = ' <b>Visit us @ </b><a href="https://evilinsult.com/">EvilInsult.com</a>';
return $url;
}
// Help Message
function getHelp()
{
$message = "
Hi, this is the official Evil Insult Generator Telegram Bot! \xf0\x9f\x98\x88 \n
Evil Insult Generator's goal is to offer the most evil insults. Please help us to reach this honorable purpose by submitting insults via mail. \n
The button Generate Insult sends you a random insult in the language you selected with the Change Language button. Default is English. \n
Clicking on Homepage gives you the link to our awesome homepage. \n
The bot supports Telegram groups and inline messages. \n
@EvilInsultGeneratorBot without any additional info shows the menu where you can send a random insult or the link to the homepage.\n
If you add a language code (e.g. @EvilInsultGeneratorBot la) it shows you a preview in that language and you can click on it to submit it.\n
In case you write an unsupported language code (e.g. @EvilInsultGeneratorBot xyz) it sends one completely random insult. \n
Questions and feedback are very welcome. \n
Take care!";
return $message;
}
?>