2018-06-24 16:52:28 +02:00
|
|
|
<?php
|
|
|
|
$bot_name = "smeagol_lotr_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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-27 17:54:15 +02:00
|
|
|
// Send html back to the sender.
|
|
|
|
function send_html($post_message, $reply=false) {
|
2018-06-24 16:52:28 +02:00
|
|
|
global $decoded;
|
|
|
|
global $bot_api;
|
|
|
|
global $chat_id;
|
|
|
|
$url = 'https://api.telegram.org/bot' . $bot_api . '/sendMessage';
|
2018-06-27 17:54:15 +02:00
|
|
|
$post_msg = array('chat_id' => $chat_id, 'text' =>$post_message, 'parse_mode' => 'html');
|
2018-06-24 16:52:28 +02:00
|
|
|
if ($reply != false) {
|
2018-07-01 11:34:39 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Edits existing pinned message
|
|
|
|
function edit_html($post_message, $reply=false) {
|
|
|
|
global $decoded;
|
|
|
|
global $bot_api;
|
|
|
|
global $chat_id;
|
|
|
|
$message_id = require("message.php");
|
|
|
|
$url = 'https://api.telegram.org/bot' . $bot_api . '/editMessageText';
|
|
|
|
$post_msg = array('chat_id' => $chat_id, 'text' =>$post_message, 'message_id' => $message_id, 'parse_mode' => 'html');
|
|
|
|
if ($reply != false) {
|
2018-06-24 16:52:28 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-06-27 17:54:15 +02:00
|
|
|
function add_requirement() {
|
|
|
|
global $command_list;
|
|
|
|
$item_id = $command_list[1];
|
|
|
|
$item_name = "";
|
|
|
|
for ($i = 2; $i < count($command_list) - 1; $i++){
|
|
|
|
$item_name .= $command_list[$i];
|
|
|
|
if ($i != count($command_list) - 2){
|
|
|
|
$item_name .= " ";
|
2018-06-24 16:52:28 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-01 12:11:57 +02:00
|
|
|
$quantity = $command_list[count($command_list) - 1];
|
2018-06-24 16:52:28 +02:00
|
|
|
|
2018-06-27 17:54:15 +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);
|
|
|
|
}
|
|
|
|
$stmt = $conn->prepare("DELETE FROM req where id = ?;");
|
2018-07-01 12:16:48 +02:00
|
|
|
$stmt->bind_param('s', $item_id);
|
2018-06-27 17:54:15 +02:00
|
|
|
$stmt->execute();
|
|
|
|
$stmt = $conn->prepare("INSERT INTO req (id, name, quantity) VALUES (?,?,?);");
|
2018-07-01 12:16:48 +02:00
|
|
|
$stmt->bind_param('ssi', $item_id, $item_name, $quantity);
|
2018-06-27 17:54:15 +02:00
|
|
|
$stmt->execute();
|
|
|
|
$conn->close();
|
|
|
|
send_html("Added to requirements");
|
2018-07-01 12:43:07 +02:00
|
|
|
get_updated_req();
|
2018-06-27 17:54:15 +02:00
|
|
|
}
|
2018-06-24 16:52:28 +02:00
|
|
|
|
2018-06-27 17:54:15 +02:00
|
|
|
function deposit(){
|
|
|
|
global $command_list;
|
|
|
|
$item_id = $command_list[1];
|
2018-07-01 11:27:59 +02:00
|
|
|
$quantity = $command_list[2];
|
2018-06-27 17:54:15 +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);
|
|
|
|
}
|
2018-07-01 11:27:59 +02:00
|
|
|
$stmt = $conn->prepare("UPDATE req set quantity = quantity - ? where id = ?;");
|
2018-07-01 12:57:20 +02:00
|
|
|
$stmt->bind_param('is', $quantity, $item_id);
|
2018-07-01 11:27:59 +02:00
|
|
|
$stmt->execute();
|
|
|
|
$conn->close();
|
|
|
|
send_html("Deposited!");
|
2018-07-01 12:43:07 +02:00
|
|
|
get_updated_req();
|
2018-06-24 16:52:28 +02:00
|
|
|
}
|
|
|
|
|
2018-07-01 11:27:59 +02:00
|
|
|
function get_updated_req() {
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
$sql = "SELECT name, quantity FROM req WHERE quantity > 0;";
|
|
|
|
$result = $conn->query($sql);
|
|
|
|
|
2018-07-01 12:48:10 +02:00
|
|
|
$text = "";
|
2018-07-01 11:27:59 +02:00
|
|
|
if ($result->num_rows > 0) {
|
|
|
|
while($row = $result->fetch_assoc()) {
|
2018-07-01 12:48:10 +02:00
|
|
|
$text .= $row["name"] . " " . $row["quantity"] . "\n";
|
2018-07-01 11:27:59 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-07-01 12:48:10 +02:00
|
|
|
$text = "Everything is complete";
|
2018-07-01 11:27:59 +02:00
|
|
|
}
|
2018-07-01 11:43:40 +02:00
|
|
|
edit_html($text);
|
2018-07-01 11:27:59 +02:00
|
|
|
}
|
2018-06-24 16:52:28 +02:00
|
|
|
// 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"};
|
|
|
|
|
|
|
|
$modules = array(
|
|
|
|
array(
|
|
|
|
"command" => "/deposit",
|
|
|
|
"function" => "deposit();"
|
|
|
|
),
|
2018-07-01 12:59:17 +02:00
|
|
|
array(
|
|
|
|
"command" => "/g_deposit",
|
|
|
|
"function" => "deposit();"
|
|
|
|
),
|
2018-06-24 16:52:28 +02:00
|
|
|
array(
|
|
|
|
"command" => "/add",
|
|
|
|
"function" => "add_requirement();"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$command_list = explode(" ", $decoded->{"message"}->{"text"});
|
|
|
|
|
|
|
|
foreach ($modules as $module ) {
|
|
|
|
if (check_command($module["command"])) {
|
|
|
|
eval($module["function"]);
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|