Skip to content

Instantly share code, notes, and snippets.

@yohanes
Last active February 17, 2021 11:59
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save yohanes/3181a5424d10ea5e2429 to your computer and use it in GitHub Desktop.
Save yohanes/3181a5424d10ea5e2429 to your computer and use it in GitHub Desktop.
telegram-webhook
<?php
include("token.php");
function request_url($method)
{
global $TOKEN;
return "https://api.telegram.org/bot" . $TOKEN . "/". $method;
}
function send_reply($chatid, $msgid, $text)
{
$data = array(
'chat_id' => $chatid,
'text' => $text,
'reply_to_message_id' => $msgid
);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents(request_url('sendMessage'), false, $context);
}
function create_response($text)
{
return "definisi " . $text;
}
function process_message($message)
{
$updateid = $message["update_id"];
$message_data = $message["message"];
if (isset($message_data["text"])) {
$chatid = $message_data["chat"]["id"];
$message_id = $message_data["message_id"];
$text = $message_data["text"];
$response = create_response($text);
send_reply($chatid, $message_id, $response);
}
return $updateid;
}
$entityBody = file_get_contents('php://input');
$message = json_decode($entityBody, true);
process_message($message);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment