Octanite-Bot/webhook.php

541 lines
14 KiB
PHP
Raw Normal View History

2018-03-31 08:52:54 +02:00
<?php
2019-01-05 19:02:43 +01:00
# vim: set tabstop=2 shiftwidth=2 expandtab:
2018-03-31 08:52:54 +02:00
$bot_name = "quadnite_bot";
$bot_api = require('api_key.php');
2018-03-31 08:52:54 +02:00
// Checks whether the given command is the same as the entered command
function check_command($command) {
global $bot_name;
global $decoded;
$command_list = explode(" ", $decoded->{"message"}->{"text"});
if ($command_list[0] == $command || $command_list[0] == $command . "@" . $bot_name) {
return True;
}
else {
return False;
}
}
// Send code back to the sender.
function send_code($post_message, $reply=false) {
global $decoded;
2018-03-31 08:52:54 +02:00
global $bot_api;
global $chat_id;
$url = 'https://api.telegram.org/bot' . $bot_api . '/sendMessage';
$post_msg = array('chat_id' => $chat_id, 'text' => '```\n ' . $post_message . '```', 'parse_mode' => 'markdown' );
if ($reply != false) {
if ($reply === true){
$post_msg['reply_to_message_id'] = $decoded->{'message'}->{'message_id'};
}
else {
$post_msg['reply_to_message_id'] = $reply;
}
}
2018-03-31 08:52:54 +02:00
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($post_msg)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
}
// Send text back to the sender.
function send_text($post_message, $reply=false) {
global $decoded;
2018-03-31 08:52:54 +02:00
global $bot_api;
global $chat_id;
$url = 'https://api.telegram.org/bot' . $bot_api . '/sendMessage';
$post_msg = array('chat_id' => $chat_id, 'text' =>$post_message );
if ($reply != false) {
if ($reply === true){
$post_msg['reply_to_message_id'] = $decoded->{'message'}->{'message_id'};
}
else {
$post_msg['reply_to_message_id'] = $reply;
}
}
2018-03-31 08:52:54 +02:00
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($post_msg)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
}
// Send html back to the sender.
function send_html($post_message, $reply=false) {
global $decoded;
2018-03-31 08:52:54 +02:00
global $bot_api;
global $chat_id;
$url = 'https://api.telegram.org/bot' . $bot_api . '/sendMessage';
$post_msg = array('chat_id' => $chat_id, 'text' =>$post_message, 'parse_mode' => 'html');
if ($reply != false) {
if ($reply === true){
$post_msg['reply_to_message_id'] = $decoded->{'message'}->{'message_id'};
}
else {
$post_msg['reply_to_message_id'] = $reply;
}
}
2018-03-31 08:52:54 +02:00
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($post_msg)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
}
// Send gif back to the sender.
function send_gif($gif_url, $reply=false) {
global $decoded;
global $bot_api;
global $chat_id;
$url = 'https://api.telegram.org/bot' . $bot_api . '/sendAnimation';
$post_msg = array('chat_id' => $chat_id, 'animation' => $gif_url );
if ($reply != false) {
if ($reply === true){
$post_msg['reply_to_message_id'] = $decoded->{'message'}->{'message_id'};
}
else {
$post_msg['reply_to_message_id'] = $reply;
}
}
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($post_msg)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
2018-08-14 14:40:59 +02:00
function forward_message($to_chat_id) {
global $decoded;
global $bot_api;
global $chat_id;
$message_id = $decoded->{"message"}->{"message_id"};
$url = 'https://api.telegram.org/bot' . $bot_api . '/forwardMessage';
$post_msg = array('chat_id' => $to_chat_id, 'message_id' => $message_id, 'from_chat_id' => $chat_id);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($post_msg)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
}
2018-03-31 08:52:54 +02:00
// Generates random words
function rand_words($onewordmode) {
global $command_list;
if($onewordmode == 1){
$num = 1;
}
else {
if (isset($command_list[1])) {
$num = $command_list[1];
}
else {
$num = 10;
}
}
$num++;
$words = array();
if (is_integer($num)) {
2019-01-05 19:02:43 +01:00
if ($num > 50) {
send_text("Too many words.", true);
return;
}
$wordlist = file("/usr/share/dict/words");
2018-03-31 08:52:54 +02:00
for ($word=1; $word < $num; $word++) {
$words[] = $wordlist[rand(0,count($wordlist))];
}
send_text(implode(' ', $words));
}
else {
2018-08-14 10:32:56 +02:00
send_text("Ever heard of numbers?", true);
2018-03-31 08:52:54 +02:00
}
}
2018-08-14 10:32:56 +02:00
function rand_question() {
2018-03-31 08:52:54 +02:00
$questions = file('rand_questions.txt');
$question = $questions[rand(0,count($questions))];
send_text($question);
}
2018-08-14 10:32:56 +02:00
function media_wiki($base_url) {
2018-03-31 08:52:54 +02:00
global $command_list;
$search_query = "";
for ($i=1; $i < count($command_list); $i++) {
$search_query .= $command_list[$i];
if ($i < count($command_list) - 1) {
$search_query .= " ";
}
}
if (preg_match('/^\s*$/', $search_query)) {
send_text('Missing search query');
return;
}
2018-06-17 14:11:43 +02:00
$url = $base_url . "?action=opensearch&format=json&search=" . urlencode($search_query);
2018-03-31 08:52:54 +02:00
$a = json_decode(file_get_contents($url));
2018-06-17 12:31:13 +02:00
$names = $a[1];
$urls = $a[3];
if (count($names) == 0) {
send_text("No result found", true);
return false;
}
$text = "Results\n";
for ($i = 0 ; $i < count($names) ; $i++){
$text .= "<a href='" . $urls[$i] . "'>" . $names[$i] . "</a>\n";
}
send_html($text);
2018-03-31 08:52:54 +02:00
}
2018-08-14 10:32:56 +02:00
function coin() {
2019-01-05 19:02:43 +01:00
$random = rand(0,1);
if ($random == 1) {
send_text('Heads', true);
}
else {
send_text('Tails', true);
}
2018-03-31 08:52:54 +02:00
}
function get_gif($force) {
if ($force){
$param = "yes";
}
else {
$param = "no";
}
$url = "https://yesno.wtf/api?force=" . $param;
$result = file_get_contents($url);
$json = json_decode($result);
$image_url = $json->{"image"};
return $image_url;
}
2018-08-14 10:32:56 +02:00
function yes_or_no() {
2018-06-16 21:43:25 +02:00
global $command_list;
if (!isset($command_list[1])){
send_text('You know, you also have to ask the question.', true);
return false;
}
$yes_replies = array("Yes", "Yep", "Yeah", "Yus", "Ja", "Ya", "Aye", "Ay", "Oui");
$no_replies = array("No", "Nopes", "Nu", "Nah", "Nein", "Naw", "Nay", "Yesn't");
$random = rand(0,1);
2018-08-14 14:40:59 +02:00
$random2 = rand(0, 10);
if ($random == 1) {
$yes = $yes_replies[array_rand($yes_replies)];
send_text($yes, true);
2018-08-14 14:40:59 +02:00
if ($random2 == 5){
send_gif(get_gif(True));
}
}
else {
$no = $no_replies[array_rand($no_replies)];
send_text($no, true);
if ($random2 == 33){
send_gif(get_gif(False));
}
}
2018-03-31 08:52:54 +02:00
}
// Kill yourself
function kys() {
global $decoded;
2018-04-02 21:13:34 +02:00
global $bot_name;
global $command_list;
$kys = file('kys.txt');
$random_kys = $kys[rand(0,count($kys)-1)];
2018-04-02 21:13:34 +02:00
if ($decoded->{'message'}->{'reply_to_message'}->{'from'}->{'username'} == $bot_name){
send_text("I can't be killed.", true);
return;
}
if (isset($decoded->{'message'}->{'reply_to_message'})) {
if (isset($decoded->{'message'}->{'reply_to_message'}->{'from'}->{'username'})){
$username = '@' . $decoded->{'message'}->{'reply_to_message'}->{'from'}->{'username'};
2018-04-02 14:40:58 +02:00
$random_kys = preg_replace('/##name##/', $username, $random_kys);
}
else {
$first_name = $decoded->{'message'}->{'reply_to_message'}->{'from'}->{'first_name'};
2018-04-02 14:40:58 +02:00
$random_kys = preg_replace('/##name##/', $first_name, $random_kys);
}
send_text($random_kys);
}
elseif (isset($command_list[1])){
$username = $command_list[1];
2018-08-08 17:00:46 +02:00
if ($username == "@quadnite_bot" || $username == "Quadnite" || $username == "quadnite"){
send_text("I can't be killed.", true);
}
else {
$random_kys = preg_replace('/##name##/', $username, $random_kys);
send_text($random_kys);
}
}
else {
2018-08-14 14:40:59 +02:00
send_text("Do you want to kill yourself?\nIf no, reply to someone with /kys to kill them or run /kys username/name.\nYou can suggest more /kys replies using /feedback", true);
2018-08-14 10:32:56 +02:00
}
}
// Insult
function send_insult() {
global $decoded;
global $bot_name;
global $command_list;
$insults = file('insults.txt');
$random_insult = $insults[rand(0,count($insults)-1)];
if ($decoded->{'message'}->{'reply_to_message'}->{'from'}->{'username'} == $bot_name){
send_text("Watch who you talk to.", true);
return;
}
if (isset($decoded->{'message'}->{'reply_to_message'})) {
if (isset($decoded->{'message'}->{'reply_to_message'}->{'from'}->{'username'})){
$username = '@' . $decoded->{'message'}->{'reply_to_message'}->{'from'}->{'username'};
$random_insult = preg_replace('/##name##/', $username, $random_insult);
}
else {
$first_name = $decoded->{'message'}->{'reply_to_message'}->{'from'}->{'first_name'};
$random_insult = preg_replace('/##name##/', $first_name, $random_insult);
}
send_text($random_insult);
}
elseif (isset($command_list[1])){
$username = $command_list[1];
if ($username == "@quadnite_bot" || $username == "Quadnite" || $username == "quadnite"){
send_text("Watch who you talk to.", true);
}
else {
$random_insult = preg_replace('/##name##/', $username, $random_insult);
send_text($random_insult);
}
}
else {
2018-08-14 14:40:59 +02:00
send_text("Do you want to insult yourself?\nIf no, reply to someone with /insult to insult them or run /insult username/name.\nYou can suggest more /kys replies using /feedback", true);
}
}
2018-08-14 14:40:59 +02:00
function feedback(){
global $command_list;
if (isset($command_list[1])){
forward_message(-1001168939936);
send_text("Thank you for the feedback");
}
else {
send_text("To send feedback type in /feedback followed by the feedback", true);
}
}
2018-08-14 10:32:56 +02:00
2018-05-27 12:27:43 +02:00
// Sends back JSON
function json() {
global $var;
2018-05-27 12:50:09 +02:00
$pretty_json = json_encode(json_decode($var), JSON_PRETTY_PRINT);
send_text($pretty_json);
2018-05-27 12:27:43 +02:00
}
2018-05-15 09:11:30 +02:00
// Start Message
function start() {
2018-08-14 14:40:59 +02:00
send_text('Hi, I am Quadnite. If you are chatting with me in private, you are most likely doing it wrong. Add me to a group for fun. To give feedback, use /feedback');
2018-05-15 09:11:30 +02:00
}
2018-12-03 07:29:19 +01:00
function rate() {
2019-01-05 19:02:43 +01:00
send_html('<a href="https://t.me/tgdrbot?start=quadnite_bot">Vote for me on Telegram Directory!</a>');
2018-12-03 07:29:19 +01:00
}
2018-07-23 19:58:50 +02:00
function help() {
2019-01-05 21:15:02 +01:00
send_html('You can either check /commands for a short overview or check the <a href="https://t.me/quadnite/9">Help Page</a>.');
2018-07-23 19:58:50 +02:00
}
2019-01-05 20:57:09 +01:00
function commands() {
send_text(file_get_contents('command_list.txt'));
}
2019-01-06 10:42:41 +01:00
function weebify() {
global $decoded;
global $command_list;
if(count($command_list) <= 1){
send_text("Need text to weebify. Send /weebify text");
return;
}
$letters = array(
"a" => "",
"b" => "",
"c" => "",
"d" => "",
"e" => "",
"f" => "",
"g" => "",
"h" => "",
"i" => "",
"j" => "",
"k" => "",
"l" => "",
"m" => "",
"n" => "𠘨",
"o" => "",
"p" => "",
"q" => "",
"r" => "",
"s" => "",
"t" => "",
"u" => "",
"v" => "",
"w" => "",
"x" => "",
"y" => "",
"z" => ""
);
2019-01-06 10:51:09 +01:00
$chars = str_split(preg_replace('/^\/[^ ]+ /', '', $decoded->{"message"}->{"text"}));
2019-01-06 10:42:41 +01:00
$text = "";
foreach($chars as $char){
if(key_exists($char, $letters)) {
$text .= $letters[$char];
}
else {
$text .= $char;
}
}
send_text($text);
}
// Get JSON from post, store it and decode it.
$var = file_get_contents('php://input');
$decoded = json_decode($var);
2018-03-31 08:52:54 +02:00
// Store the chat ID
$chat_id = $decoded->{"message"}->{"chat"}->{"id"};
$modules = array(
2018-05-15 09:11:30 +02:00
array(
"command" => "/start",
"function" => "start();"
),
2018-03-31 08:52:54 +02:00
array(
"command" => "/word",
"function" => "rand_words(1);"
),
array(
"command" => "/words",
"function" => "rand_words(0);"
),
array(
"command" => "/question",
"function" => "rand_question();"
),
2018-06-17 14:11:43 +02:00
array(
"command" => "/arch_wiki",
"function" => "media_wiki('https://wiki.archlinux.org/api.php');"
),
2018-03-31 08:52:54 +02:00
array(
"command" => "/wiki",
2018-06-17 14:11:43 +02:00
"function" => "media_wiki('https://en.wikipedia.org/w/api.php');"
2018-03-31 08:52:54 +02:00
),
array(
"command" => "/coin",
"function" => "coin();"
),
array(
"command" => "/is",
"function" => "yes_or_no();"
),
2018-04-02 22:43:43 +02:00
array(
"command" => "/are",
"function" => "yes_or_no();"
),
2018-03-31 08:52:54 +02:00
array(
"command" => "/can",
"function" => "yes_or_no();"
),
array(
"command" => "/will",
"function" => "yes_or_no();"
),
array(
"command" => "/shall",
"function" => "yes_or_no();"
),
array(
"command" => "/was",
"function" => "yes_or_no();"
),
array(
"command" => "/does",
"function" => "yes_or_no();"
),
array(
"command" => "/did",
"function" => "yes_or_no();"
),
array(
"command" => "/should",
"function" => "yes_or_no();"
),
2018-04-07 12:14:36 +02:00
array(
"command" => "/do",
"function" => "yes_or_no();"
),
array(
"command" => "/kys",
"function" => "kys();"
2018-05-27 12:27:43 +02:00
),
array(
"command" => "/json",
"function" => "json();"
2018-07-23 19:58:50 +02:00
),
array(
"command" => "/help",
"function" => "help();"
2018-08-14 10:32:56 +02:00
),
array(
"command" => "/insult",
"function" => "send_insult();"
2018-08-14 14:40:59 +02:00
),
array(
"command" => "/feedback",
"function" => "feedback();"
2018-12-03 07:29:19 +01:00
),
array(
"command" => "/rate",
"function" => "rate();"
2019-01-05 20:57:09 +01:00
),
array(
"command" => "/commands",
2019-01-05 21:00:26 +01:00
"function" => "commands();"
2019-01-06 10:44:04 +01:00
),
array(
"command" => "/weebify",
"function" => "weebify();"
2018-03-31 08:52:54 +02:00
)
);
2018-04-02 10:21:13 +02:00
if (!isset($decoded->{"message"}->{"text"})){
2019-01-05 19:02:43 +01:00
exit();
2018-04-02 10:21:13 +02:00
}
2018-03-31 08:52:54 +02:00
if (isset($decoded->{"message"}->{"pinned_message"})){
2019-01-05 19:02:43 +01:00
exit();
2018-03-31 08:52:54 +02:00
}
$command_list = explode(" ", $decoded->{"message"}->{"text"});
foreach ($modules as $module ) {
if (check_command($module["command"])) {
eval($module["function"]);
exit();
}
}
?>