Skip to content

Instantly share code, notes, and snippets.

@Kyle2142
Last active April 4, 2018 16:36
Show Gist options
  • Save Kyle2142/5e3ff00e67404e7d8f4e7f8a1d41ac2d to your computer and use it in GitHub Desktop.
Save Kyle2142/5e3ff00e67404e7d8f4e7f8a1d41ac2d to your computer and use it in GitHub Desktop.
t.me/revolver_ocelot_bot
<?php
$IMGID = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; //image ID given to us by telegram
$content = file_get_contents("php://input"); //get json from telegram's request
$update = json_decode($content, TRUE); //decode json into assoc array
//file_put_contents('ocelotbot.log',print_r($update,true),FILE_APPEND); //log updates for debugging purposes
if(isset($update['inline_query'])){
//$query = $update['inline_query']['query']; //text from user
$qid = $update['inline_query']['id']; //query id
$result = [
[
'type'=>'photo',
'id'=>0,
'photo_file_id'=>$IMGID,
'title'=>'Revolver Ocelot',
'description'=>'Revolver Ocelot',
'caption'=>'Revolver Ocelot',
'reply_markup'=>['inline_keyboard' =>
[
[ //one row
[//one column
'text' => "Revolver Ocelot",
'callback_data'=>'Revolver Ocelot'
]
]//add another array here for vertical append
]
]
]
];
api::answerinline($qid, $result, ['cache_time'=>860000]);
}elseif(isset($update['callback_query'])){
$query = $update['callback_query']; //easy access to array
api::answercallback($query['id'],
[
'text'=>"Revolver Ocelot\n(Revolver Ocelot)",
'show_alert'=>0,
'cache_time'=>860000
]
);
}elseif(isset($update['message'])){
$uid = $update['message']['from']['id'];
$keyboard = json_encode([
'inline_keyboard'=>[
[
[
'text'=>'Revolver Ocelot',
'switch_inline_query'=>''
]
]
]
]);
api::quickphoto($uid, $IMGID,"Revolver Ocelot",['reply_markup'=>$keyboard]);
}
class api{
static $API = "https://api.telegram.org/botTOKEN/"; //Official
public static function quickaction($method,$params){ //can only be used once per request from telegram
if(!is_array($params)) return false;
header_remove();
http_response_code(200);
header('Content-Type: application/json');
header('Status: 200 OK');
$params['method']=$method;
echo json_encode($params);
}
//inline mode only
public static function answerinline($qid,$results,$extras=[]){
api::quickaction("answerinlinequery",array_merge(['inline_query_id'=>$qid,'results'=>json_encode($results)],$extras));
}
//callback_query
public static function answercallback($qid,$params=[]){
$params['callback_query_id']=$qid;
api::quickaction("answercallbackquery",$params);
}
//Send photo
public static function quickphoto($chat_id, $photo, $text='', $extras=[]){
api::quickaction('sendphoto',array_merge(['chat_id'=>$chat_id, 'photo'=>$photo, 'caption'=>$text], $extras));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment