Telegram Bot code to whitelist a member of private group - scripting

I create a private supergroup in Telegram and want to only allow certain telegram userid or phone number to be able to join.
Any unknown telegram userid/number that is not in a whitelist file of the bot will be automatically kicked.
Maybe store a whitelist in a file and run the bot in Cloud Server? There is an API to kick a member https://core.telegram.org/bots/api#kickchatmember but I don't know the webhook to trigger adding member event.
Anyone mind to code a telegram bot script for this? Sorry for inconvenience.

try this
<?php
$API_KEY = 'token';
define('API_KEY', $API_KEY);
function bot($method,$datas=[]){
$url = "https://api.telegram.org/bot" . API_KEY . "/" . $method;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);
$res = curl_exec($ch);
if (curl_error($ch)) {
var_dump(curl_error($ch));
} else {
return json_decode($res);
}
}
$update = json_decode(file_get_contents('php://input'));
$message = $update->message;
$text = $message->text;
$chat_id = $message->chat->id;
$from_id = $message->from->id;
$new_member = $message->new_chat_member->id;
$memberid = file_get_contents('whitelist.txt'); //put userid in whitelist.txt
$whitelist = explode("\n", $memberid);
if ($new_member) {
if (!in_array($chat_id, $whitelist)) {
bot('kickChatMember',[
'chat_id'=>$chat_id,
'user_id'=>$message->new_chat_member->id]);
}
}

Related

Instagram basic display api withlaravel

I'm working on Instagram basic api, in my project user can connect their Instagram feed to our project. For that I'm generating a short live authentication code but could not get the long live access code. Its returning null.
Here is my code for getting long live access code.
$client_secret = "###############";
$client_id = "###############";
$redirect_uri = '#########';
$short_access_token = "####"; // getting this from another function
$ig_rtu = 'https://api.instagram.com/oauth/access_token?client_id='.$client_id.'&client_secret='.$client_secret.'&grant_type=authorization_code&redirect_uri='.$redirect_uri.'&access_token='.$short_access_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ig_rtu);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$ig_new = curl_exec($ch);
curl_close ($ch);
return $ig_new = json_decode($ig_new, true);
In postman this works fine but I could not get it done in Laravel.
What I'm missing here ?
Any lead is appreciated.
I've done this in 3 steps. I will leave the code out for step 1 and include my implementation for step 2 and 3.
Steps:
get the code
get the short lived token using the code
get the long lived token using the short lived token
Here are the public variables used.
public $appId = null;
public $appSecret = null;
public $redirectUrl = null;
// Instagram request data
public $socialUserId = null;
public $socialAuthCode = null;
public $socialUserName = null;
public $socialShortAccessToken = null; // 1 hour
public $socialLongAccessToken = null; // 60 days
public $socialLongAccessTokenExpiresInSeconds = null; // 5183910 seconds || 59.99896 days - relative to date created
Step 2:
public function getUserIdAndShortAccessToken(){
/* Get UserId and access Token
https://api.instagram.com/oauth/access_token \
-F client_id={app-id} \
-F client_secret={app-secret} \
-F grant_type=authorization_code \
-F redirect_uri={redirect-uri} \
-F code={code}
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
'https://api.instagram.com/oauth/access_token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$post = array(
'client_id' => $this->appId,
'client_secret' => $this->appSecret,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirectUrl,
'code' => $this->socialAuthCode,
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$curlResult = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
curl_close($ch);
return;
}else{
curl_close($ch);
}
$this->socialUserId = json_decode($curlResult)->user_id;
$this->socialShortAccessToken = json_decode($curlResult)->access_token;
}
Step 3:
public function getLongAccessToken(){
/* Get long-lived token
curl -i -X GET "https://graph.instagram.com/access_token
?grant_type=ig_exchange_token
&client_secret={instagram-app-secret}
&access_token={short-lived-access-token}"
*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.instagram.com/access_token?grant_type=ig_exchange_token&client_secret='
.$this->appSecret.'&access_token='
.$this->socialShortAccessToken
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlResult = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
curl_close($ch);
return;
}else{
curl_close($ch);
}
$this->socialLongAccessToken = json_decode($curlResult)->access_token;
$this->socialLongAccessTokenExpiresInSecond = json_decode($curlResult)->expires_in;
}

One click login to WHM

I'm trying to generate a one-click login URL for WHM.
The code below was one based on a cPanel login (which I was going to change for WHM), but it didn't work at all. Access denied errors.
Here's what I tried:
$whmusername = 'root';
$whmpassword = 'password';
$hostname = 'server.example.com';
$cpanel_user = 'cpaneluser';
$query = "https://$hostname/json-api/create_user_session?api.version=1&user=$cpanel_user&service=cpaneld";
$curl = curl_init(); // Create Curl Object.
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // Allow self-signed certificates...
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // and certificates that don't match the hostname.
curl_setopt($curl, CURLOPT_HEADER, false); // Do not include header in output
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Return contents of transfer on curl_exec.
$header[0] = "Authorization: Basic " . base64_encode($whmusername.":".$whmpassword) . "\n\r";
curl_setopt($curl, CURLOPT_HTTPHEADER, $header); // Set the username and password.
curl_setopt($curl, CURLOPT_URL, $query); // Execute the query.
$result = curl_exec($curl);
if ($result == false) {
error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
}
$decoded_response = json_decode( $result, true );
$session_url = $decoded_response['data']['url'];
$cookie_jar = 'cookie.txt';
curl_setopt($curl, CURLOPT_HTTPHEADER, null); // Unset the authentication header.
curl_setopt($curl, CURLOPT_COOKIESESSION, true); // Initiate a new cookie session.
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar); // Set the cookie jar.
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar); // Set the cookie file.
curl_setopt($curl, CURLOPT_URL, $session_url); // Set the query url to the session login url.
$result = curl_exec($curl); // Execute the session login call.
if ($result == false) {
error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
// Log an error if curl_exec fails.
}
$session_url = preg_replace( '{/login(?:/)??.*}', '', $session_url );
$query = "$session_url/execute/Ftp/list_ftp";
curl_setopt($curl, CURLOPT_URL, $query); // Change the query url to use the UAPI call.
$result = curl_exec($curl); // Execute the UAPI call.
if ($result == false) {
error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
// log error if curl exec fails
}
curl_close($curl);
print $result;
I was able to do this by using this following code. This code was originally used in a WHM API example, but I reapplied it for this purpose.
$user = "root";
$token = "API Token";
$query = "https://server.example.com:2087/json-api/create_user_session?api.version=1&user=root&service=whostmgrd";
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
$header[0] = "Authorization: whm $user:$token";
curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
curl_setopt($curl, CURLOPT_URL, $query);
$result = curl_exec($curl);
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($http_status != 200) {
echo "[!] Error: " . $http_status . " returned\n";
} else {
$json = json_decode($result, true);
$login_url = $json['data']['url'];
echo $login_url;
}
curl_close($curl);
Instead of using WHM user/password, use just the user and login with an API Token which can be created in WHM -> Manage API Tokens.
Then take the json data and output just the URL.

How to make separate config file for sms sending

I have done a sms sending code in codeigniter. It is worked successfully. But actually i want to make a config file so that i will not write the username, password, senderid on each page.
Here is my code below. First i have done a library file for sms sending.
Sms.php
<?php
class SMS
{
function SendSMS($url)
{
if(function_exists('curl_init'))
{
$ch = curl_init();
$timeout = 60;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);
$data = curl_exec($ch);
if($data === FALSE){
throw new Exception(curl_errno($ch));
}
curl_close($ch);
return $data;
}
else
{
return false;
}
}
}
?>
Again for sms sending i am writting code in each controller something like this.
$sms_username = "GAPSMS";
$sms_password = "GAPSMS";
$sms_senderid = "GAPSMS";
$sms_mobile = $mobile;
$sms_message = urlencode('Your One Time Password for transaction is: '.$otp);
$sms_api = "http://sendsms.sandeshwala.com/API/WebSMS/Http/v1.0a/index.php?username=$sms_username&password=$sms_password&sender=$sms_senderid&to=$sms_mobile&message=$sms_message&reqid=1&format={json|text}";
$this->sms->SendSMS($sms_api);
This much below code i want to write in a config file in such a way so that i can write this once and then can use in every controller to send sms.
$sms_username = "GAPSMS";
$sms_password = "GAPSMS";
$sms_senderid = "GAPSMS";
UPDATED
In CodeIgniter, libraries can have their own configuration file. Let's change your library a bit:
<?php
class Sms
{
private $_username = 'GAPSMS'; // default value
private $_password = 'GAPSMS'; // default value
private $_senderid = 'GAPSMS'; // default value
/**
* Class constructor so the config
* file is loaded.
*/
public function __construct($config = array())
{
if ( ! empty($config))
{
foreach ($config as $key => $val)
{
$this->{"_".$key} = $val;
}
}
}
function send($mobile, $message)
{
if(function_exists('curl_init'))
{
$url = 'http://sendsms.sandeshwala.com/API/WebSMS/Http/v1.0a/index.php?username='.$this->_username.'&password='.$this->_password.'&sender='.$this->_senderid.'&to='.$mobile.'&message='.$message.'&reqid=1&format={json|text}';
$ch = curl_init();
$timeout = 60;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);
$data = curl_exec($ch);
if($data === FALSE){
throw new Exception(curl_errno($ch));
}
curl_close($ch);
return $data;
}
else
{
return false;
}
}
}
Then go create the config file application/config/sms.php inside which you put:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$config['username'] = 'GAPSMS';
$config['password'] = 'GAPSMS';
$config['senderid'] = 'GAPSMS';
// End of file.
Now whenever the library is loaded, and config file is loaded as well as values are set, everything should work just fine. And the rest of the code would be:
// Load the library and simply pass mobile number and message.
$this->load->library('sms');
$data = $this->sms->send('0123456789', 'Hello, this is the message');

Moodle api list all cohorts

Im looking to get all cohorts, i can get the info if i have the id but i want all the cohorts names and ids. Please let me know if theres a way to do this. Thanks in advance
$functionname = 'core_cohort_get_cohorts';
$cohortIDS = array( '1' );
$data_string = http_build_query(array('cohortids' => $cohortIDS));
$utoken = 'mytoken';
$adduser = 'yoururl/webservice/rest/server.php? wstoken='.$utoken.'&wsfunction='.$functionname.'&moodlewsrestformat=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$adduser);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
echo '<pre>';
print_r(json_decode($server_output));
echo '</pre>';
You could add more method to core_cohort to get all cohort as the following:
In the cohort/externallib.php, add more method:
public static function get_all_cohorts(){
global $DB;
$cohortids = $DB->get_records('cohort', null, null, 'id');
$arrids = array();
foreach($cohortids as $id){
$arrids[] = $id->id;
}
return (new core_cohort_external())->get_cohorts($arrids);
}
In addition, you have to register this webservice called 'core_cohort_get_all_cohorts' to call this webservice from outside. Please tell me if you have further question.

Facebook API call- "This API version is deprecated"

I'm trying to call to Photo.upload on the the Facebook API server. As far as I can tell the code to construct the call is good but I can't understand the responce I'm getting back from the server. As far as I can see, this call is ment to work and other people don't get this problem. I can only reason that something is wrong with the code. The commented stuff is an artifact of me trying different things to get a different responce from the server. The original code that I've changed was in part taken from an example of how to do this which I couldn't get to work either really:
http://www.jaisenmathai.com/blog/2008/11/27/using-the-facebook-api-to-upload-photos/
Server Responce:
12 This API version is deprecated method photos.upload api_key b92cee19a33c861275bfce4695896e44 call_id 1250194789.61 garden_jpg /var/www/vivaladan/pictureyourselfhull/garden.jpg v 0 sig 896ee95339cad24ce7e64a05ca764123
Code:
$key = b92cee19a33c861275bfce4695896e44;
$ver = 1.0;
$cid = microtime(true);
$uid = BIGINT;
$file= "garden.jpg";
$args = array(
//amethod => photos.upload,
v => $ver,
api_key => $key,
//uid => $uid,
call_id => $cid,
//format => XML
);
$args[basename($file)] = realpath($file);
signRequest($args,$sec);
$postString = "";
foreach($args as $index => $value) {
$postString .= $index ."=".$value."&";
}
$postString = trim($postString, '&');
$ch = curl_init();
$url = "http://api.facebook.com/restserver.php?method=photos.upload";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
$data = curl_exec($ch);
echo $data;
function signRequest(&$args, $secret){
ksort($args);
$sig = "";
foreach($args as $k => $v){
$sig .= $k . '=' . $v;
}
$sig .= $secret;
$args[sig] = md5($sig);
}
Rest API call is not working anymore.
Try GraphApi
I'm guessing it's because you're using $ver = 0.0; - there's no 0.0 version of the API.
try using api.new.facebook.com API URL instead