Skip to content

Commit

Permalink
Create fungsi.php
Browse files Browse the repository at this point in the history
  • Loading branch information
rapdodge authored Jan 5, 2020
1 parent 21342ff commit 8c31003
Showing 1 changed file with 198 additions and 0 deletions.
198 changes: 198 additions & 0 deletions fungsi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php
if (!defined('RA')) {
die('Tidak boleh diakses langsung.');
}
function myPre($value)
{
echo '<pre>';
print_r($value);
echo '</pre>';
}

define('BOT_TOKEN', 'TOKEN_BOT_MU_DISINI');
// versi official telegram bot
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
define('myVERSI','0.03');
// aktifkan ini jika ingin menampilkan debugging poll
$debug = false;
function exec_curl_request($handle) {
$response = curl_exec($handle);
if ($response === false) {
$errno = curl_errno($handle);
$error = curl_error($handle);
error_log("Curl returned error $errno: $error\n");
curl_close($handle);
return false;
}
$http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
curl_close($handle);
if ($http_code >= 500) {
// do not wat to DDOS server if something goes wrong
sleep(10);
return false;
} else if ($http_code != 200) {
$response = json_decode($response, true);
error_log("Request has failed with error {$response['error_code']}: {$response['description']}\n");
if ($http_code == 401) {
throw new Exception('Invalid access token provided');
}
return false;
} else {
$response = json_decode($response, true);
if (isset($response['description'])) {
error_log("Request was successfull: {$response['description']}\n");
}
$response = $response['result'];
}
return $response;
}
function apiRequest($method, $parameters=null) {
if (!is_string($method)) {
error_log("Method name must be a string\n");
return false;
}
if (!$parameters) {
$parameters = array();
} else if (!is_array($parameters)) {
error_log("Parameters must be an array\n");
return false;
}
foreach ($parameters as $key => &$val) {
// encoding to JSON array parameters, for example reply_markup
if (!is_numeric($val) && !is_string($val)) {
$val = json_encode($val);
}
}
$url = API_URL.$method.'?'.http_build_query($parameters);
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($handle, CURLOPT_TIMEOUT, 60);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
return exec_curl_request($handle);
}
function apiRequestJson($method, $parameters) {
if (!is_string($method)) {
error_log("Method name must be a string\n");
return false;
}
if (!$parameters) {
$parameters = array();
} else if (!is_array($parameters)) {
error_log("Parameters must be an array\n");
return false;
}
$parameters["method"] = $method;
$handle = curl_init(API_URL);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($handle, CURLOPT_TIMEOUT, 60);
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($parameters));
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
return exec_curl_request($handle);
}
// jebakan token, klo ga diisi akan mati
if (strlen(BOT_TOKEN)<20)
die(PHP_EOL."-> -> Token BOT API nya mohon diisi dengan benar!\n");


function sendApiPhoto($chatid, $photo, $caption = null, $reply_to_message_id = null, $reply_markup = null)
{
$method = 'sendPhoto';
$data = ['chat_id' => $chatid,
'photo' => $photo];
$result = apiRequest($method, $data);
}

function sendApiMsg($chatid, $text, $msg_reply_id = false, $parse_mode = true, $disablepreview = false)
{
$method = 'sendMessage';
$data = ['chat_id' => $chatid, 'text' => $text, 'parse_mode' => 'Markdown',];
if ($msg_reply_id) {
$data['reply_to_message_id'] = $msg_reply_id;
}
//if ($parse_mode) {
// $data['parse_mode'] = $parse_mode;
//}
if ($disablepreview) {
$data['disable_web_page_preview'] = $disablepreview;
}
$result = apiRequest($method, $data);
}
function sendApiAction($chatid, $action)
{
$method = 'sendChatAction';
$data = [
'chat_id' => $chatid,
'action' => $action,
];
$result = apiRequest($method, $data);
}
function sendApiKeyboard($chatid, $text, $keyboard = [], $inline = false)
{
$method = 'sendMessage';
$replyMarkup = [
'keyboard' => $keyboard,
'resize_keyboard' => true,
'one_time_keyboard' => true,
];
$data = [
'chat_id' => $chatid,
'text' => $text,
'parse_mode' => 'Markdown',
];
$inline
? $data['reply_markup'] = json_encode(['inline_keyboard' => $keyboard])
: $data['reply_markup'] = json_encode($replyMarkup);
$result = apiRequest($method, $data);
}
function editMessageText($chatid, $message_id, $text, $keyboard = [], $inline = false)
{
$method = 'editMessageText';
$replyMarkup = [
'keyboard' => $keyboard,
'resize_keyboard' => true,
];
$data = [
'chat_id' => $chatid,
'message_id' => $message_id,
'text' => $text,
'parse_mode' => 'Markdown',
];
$inline
? $data['reply_markup'] = json_encode(['inline_keyboard' => $keyboard])
: $data['reply_markup'] = json_encode($replyMarkup);
$result = apiRequest($method, $data);
}
function sendApiHideKeyboard($chatid, $text)
{
$method = 'sendMessage';
$data = [
'chat_id' => $chatid,
'text' => $text,
'parse_mode' => 'Markdown',
'reply_markup' => json_encode(['hide_keyboard' => true]),
];
$result = apiRequest($method, $data);
}
function sendApiSticker($chatid, $sticker, $msg_reply_id = false)
{
$method = 'sendSticker';
$data = [
'chat_id' => $chatid,
'sticker' => $sticker,
];
if ($msg_reply_id) {
$data['reply_to_message_id'] = $msg_reply_id;
}
$result = apiRequest($method, $data);
}
function deleteMsg($chatid, $msgid)
{
$method = 'deleteMessage';
$data = [
'chat_id' => $chatid,
'message_id' => $msgid,
];
$result = apiRequest($method, $data);
}

0 comments on commit 8c31003

Please sign in to comment.