Zeeth-Naaw-Bot/webhook.php

190 lines
5.6 KiB
PHP
Raw Normal View History

<?php
$bot_name = "zeeth_naaw_bot";
$bot_api = require('api_key.php');
// 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 html back to the sender.
function send_html($post_message, $reply=false) {
global $decoded;
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;
}
}
$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);
}
function new_member() {
global $decoded;
2018-05-17 11:43:18 +02:00
$mysql = require('mysql_credentials.php');
$conn = new mysqli($mysql['servername'], $mysql['username'], $mysql['password'], $mysql['database']);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
foreach ($decoded->{'message'}->{'new_chat_members'} as $member){
2018-04-05 07:34:45 +02:00
if ($member->{'is_bot'}) {
continue;
}
$username = $member->{"username"};
$user_id = $member->{"id"};
$query = "INSERT INTO users (user_id, username, follows) values($user_id, '$username', -1);";
$conn->query($query);
$lastmember = include('lastmember.php');
$text = "Welcome @$username,\n";
$text .= "\n";
$text .= "Congratulations for following the chain all the way to here.\n";
$text .= "To get started, read the rules @Bio_Chain_2_Rules and add <pre>@$lastmember</pre> to your bio.\n";
$text .= "You can run /update to regenerate the chain.\n";
$text .= "\n";
$text .= "Have Fun";
2018-04-05 08:39:08 +02:00
send_html($text);
}
$conn->close();
2018-04-05 07:34:45 +02:00
}
function member_exit() {
global $decoded;
$mysql = require('mysql_credentials.php');
$conn = new mysqli($mysql['servername'], $mysql['username'], $mysql['password'], $mysql['database']);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$user = $decoded->{"message"}->{"left_chat_member"};
$user_id = $user->{"id"};
$username = $user->{"username"};
$follows_return = $conn->query("select * from users where user_id = $user_id;");
if ($follows_return->num_rows == 0){
exit();
}
$follows = $follows_return->fetch_assoc()["follows"];
if ($follows != -1){
$follows_username = $conn->query("select * from users where user_id = $follows;")->fetch_assoc()['username'];
}
$followed_by_return = $conn->query("select * from users where follows = $user_id;");
if ($followed_by_return->num_rows == 0){
exit();
}
$followed_by_return->fetch_assoc();
if ($followed_by_return->num_rows > 0) {
if ($follows = -1) {
$text = "$username left. They had no username in his bio. The following user(s) pointed to they:\n";
}
else {
$text = "$username left. They had <pre>$follows_username</pre> username in his bio. The following user(s) pointed to they:\n";
}
while ($row = $followed_by_return->fetch_assoc()){
$text .= $row["username"] . "\n";
}
}
else {
if ($follows = -1) {
$text = "$username left. They had no username in his bio. No user pointed to they.";
}
else {
$text = "$username left. They had <pre>$follows_username</pre> username in his bio. No user pointed to they.";
}
}
send_html($text);
$conn->query("delete from users where user_id = $user_id;");
$conn->close();
}
2018-04-05 08:39:08 +02:00
2018-04-05 07:34:45 +02:00
function update() {
2018-04-05 08:39:08 +02:00
send_html("Update started. New chain will only be send if the chain has changed. Please wait as this takes about a minute.");
exec('echo php ' . __DIR__ . '/update_chain.php send | at now');
}
2018-04-05 07:34:45 +02:00
2018-04-25 11:01:42 +02:00
function allchains() {
exec('echo php ' . __DIR__ . '/update_chain.php all | at now');
}
2018-04-23 16:10:20 +02:00
function dnf() {
$mysql = require('mysql_credentials.php');
$conn = new mysqli($mysql['servername'], $mysql['username'], $mysql['password'], $mysql['database']);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT username from users where follows = -1 ;";
$result = $conn->query($query);
$text = "The following users follow no other user. \n";
while ($row = $result->fetch_assoc()) {
$text .= $row['username'] . "\n";
}
2018-04-23 16:14:51 +02:00
send_html($text);
2018-04-23 16:10:20 +02:00
$conn->close();
}
// Get JSON from post, store it and decode it.
$var = file_get_contents('php://input');
$decoded = json_decode($var);
// Store the chat ID
$chat_id = $decoded->{"message"}->{"chat"}->{"id"};
if ($chat_id != -1001180504638){
die();
}
$modules = array(
array(
"command" => "/update",
"function" => "update();"
2018-04-23 16:10:20 +02:00
),
array(
"command" => "/doesnotfollow",
"function" => "dnf();"
2018-04-25 11:01:42 +02:00
),
array(
"command" => "/allchains",
"function" => "allchains();"
)
);
$command_list = explode(" ", $decoded->{"message"}->{"text"});
# Run new_member function for a new member
if (isset($decoded->{'message'}->{'new_chat_members'})) {
new_member();
}
# Run member_exit function when a member leaves
if (isset($decoded->{'message'}->{"left_chat_member"})) {
member_exit();
}
foreach ($modules as $module ) {
if (check_command($module["command"])) {
eval($module["function"]);
exit();
}
}
?>