Add basecode.

Added send_html(), add_to_db() and base code for dealing with the webhook.
This commit is contained in:
Ceda EI 2018-07-22 01:52:48 +05:30
parent 33f09bc011
commit 82fdb73ad7
1 changed files with 62 additions and 0 deletions

62
webhook.php Normal file
View File

@ -0,0 +1,62 @@
<?php
$bot_name = "smeagol_lotr_bot";
$bot_api = require('api_key.php');
// 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 add_to_db($success) {
global $decoded;
$user_id = $decoded->{"message"}->{"from"}->{"id"};
$timestamp = $decoded->{"message"}->{"forward_date"};
$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("INSERT INTO data (id, timestamp, success) VALUES (?,?,?);");
$stmt->bind_param('iii', $user_id, $timestamp, $success);
$stmt->execute();
$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 ($decoded->{"message"}->{"forward_from"}->{"username"} != "chtwrsbot") {
exit();
}
if (!isset($decoded->{"message"}->{"text"})){
exit();
}
$text = $decoded->{"message"}->{"text"};
?>