Last active
May 11, 2016 21:51
-
-
Save pichsenmeister/fbdce9ebff710b93b64d4483986bbbc6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$token = '225242302:AAFJiXd3dSAbLAkN18MhRIGp9ZsIox5fRkM'; | |
function sendAPIRequest($token, $method, $payload) | |
{ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, "https://api.telegram.org/bot$token/$method"); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); | |
$result = curl_exec($ch); | |
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
logger($result); | |
} | |
function logger($data) | |
{ | |
$data = var_export($data, true); | |
var_dump($data); | |
} | |
$payload = json_decode(file_get_contents('php://input'), true); | |
$prettyJson = json_encode($payload, JSON_PRETTY_PRINT); | |
logger($prettyJson); | |
if (array_key_exists('inline_query', $payload)) { | |
handleInline($payload['inline_query']); | |
} elseif (array_key_exists('message', $payload)) { | |
handleMessage($payload['message']); | |
} | |
http_response_code(202); | |
function handleInline($query) | |
{ | |
global $token; | |
$results = getResults(); | |
$payload = [ | |
'inline_query_id' => $query['id'], | |
'results' => json_encode( | |
array_slice($results, 0, strlen($query['query'])) | |
), | |
]; | |
sendAPIRequest($token, 'answerInlineQuery', $payload); | |
} | |
function handleMessage($message) | |
{ | |
global $token; | |
$payload = [ | |
'chat_id' => $message['chat']['id'], | |
'text' => $message['text'], | |
]; | |
$payload['reply_markup'] = json_encode( | |
['keyboard' => [ | |
['row 1 col 1', 'row 1 col 2'], | |
['row 2'], | |
], | |
'resize_keyboard' => true, ] | |
); | |
sendAPIRequest($token, 'sendMessage', $payload); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment