Add update_chain.php and mysql_credentials.php.sample

Added send_code, get_chain_from_user functions. The get_chain_from_user generates a chain from a given user and returns an array in reverse sequential order.
This commit is contained in:
Ceda EI 2018-04-03 19:17:45 +05:30
parent 3cfd25ea10
commit fa39901f8e
2 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,8 @@
<?php
return array(
"servername" => "localhost",
"username" => "username_goes_here",
"password" => "password_goes_here",
"database" => "database_name_goes_here"
);
?>

56
update_chain.php Normal file
View File

@ -0,0 +1,56 @@
<?php
$bot_api = require('api_key.php');
$chat_id = -1001180504638;
$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);
}
function send_code($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' => '```\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;
}
}
$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 get_chain_from_user($user) {
global $conn;
$output = array($user);
$last_user_id = $user['user_id'];
while (true) {
$query = "SELECT user_id, username from users where follows = $last_user_id";
$result = $conn->query($query);
if ($result->num_rows > 0){
# Code executed if this isn't the last user
$details = $result->fetch_assoc();
array_push($output, $details );
$last_user_id = $details['user_id'];
}
else {
break;
}
}
return $output;
}
$conn->close();
?>