با سلام و احترام،
وقتتون بخیر دوستان،
برای ایجاد ربات تلگرام مراحل زیر را انجام داده ام:
1-ایجاد ربات از طریق BotFather
2- فعال کردن ssl روی سابدامین
3- ست کردن وب هوک برای ربات تلگرام از طریق url زیر:
https://api.telegram.orgbot739887721:AAEjq-7TMEv3MuOQu-v6pKDh0aRBkwutGNUsetwebhook?url=https://bot-test.trefah.ir/bot-sabt.php
4- محتوای فایل bot-sabt.php:
define('BOT_TOKEN', '739887721:AAEjq-7TMEv3MuOQu-v6pKDh0aRBkwutGNU');
define('APIURL', 'https://api.telegram.org/bot'.BOTTOKEN.'/');
function apiRequestWebhook($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;
header("Content-Type: application/json");
echo json_encode($parameters);
return true;
}
function execcurlrequest($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;
}
$httpcode = intval(curlgetinfo($handle, CURLINFOHTTPCODE));
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);
errorlog("Request has failed with error {$response['errorcode']}: {$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) {
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 (!isnumeric($val) && !isstring($val)) {
$val = json_encode($val);
}
}
$url = APIURL.$method.'?'.httpbuild_query($parameters);
$handle = curl_init($url);
curlsetopt($handle, CURLOPTRETURNTRANSFER, true);
curlsetopt($handle, CURLOPTCONNECTTIMEOUT, 5);
curlsetopt($handle, CURLOPTTIMEOUT, 60);
return execcurlrequest($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 = curlinit(APIURL);
curlsetopt($handle, CURLOPTRETURNTRANSFER, true);
curlsetopt($handle, CURLOPTCONNECTTIMEOUT, 5);
curlsetopt($handle, CURLOPTTIMEOUT, 60);
curlsetopt($handle, CURLOPTPOSTFIELDS, json_encode($parameters));
curlsetopt($handle, CURLOPTHTTPHEADER, array("Content-Type: application/json"));
return execcurlrequest($handle);
}
function processMessage($message) {
// process incoming message
$messageid = $message['messageid'];
$chat_id = $message['chat']['id'];
if (isset($message['text'])) {
// incoming text message
$text = $message['text'];
if (strpos($text, "/start") === 0) {
apiRequestJson("sendMessage", array('chatid' => $chatid, "text" => 'Hello', 'reply_markup' => array(
'keyboard' => array(array('Hello', 'Hi')),
'onetimekeyboard' => true,
'resize_keyboard' => true)));
} else if ($text === "Hello" || $text === "Hi") {
apiRequest("sendMessage", array('chatid' => $chatid, "text" => 'Nice to meet you'));
} else if (strpos($text, "/stop") === 0) {
// stop now
} else {
apiRequestWebhook("sendMessage", array('chatid' => $chatid, "replytomessageid" => $messageid, "text" => 'Cool'));
}
} else {
apiRequest("sendMessage", array('chatid' => $chatid, "text" => 'I understand only text messages'));
}
}
define('WEBHOOK_URL', 'https://my-site.example.comsecret-path-for-webhooks');
if (phpsapiname() == 'cli') {
// if run from console, set or delete webhook
apiRequest('setWebhook', array('url' => isset($argv[1]) && $argv[1] == 'delete' ? '' : WEBHOOK_URL));
exit;
}
$content = filegetcontents("php://input");
$update = json_decode($content, true);
if (!$update) {
// receive wrong update, must not happen
exit;
}
if (isset($update["message"])) {
processMessage($update["message"]);
}
لطفاْ راهنمایی بفرمایید.
این پست در تاریخ {{ dateString(new Date(post.deleteDate)) }} توسط {{ post.deletedByUser }} حذف شده است.
دلیل حذف: {{ post.deleteReason ?? 'نامشخص' }}
زمان پاسخ گویی روز های شنبه الی چهارشنبه ساعت 9 الی 18
فقط به موضوعات مربوط به محصولات آموزشی و فروش پاسخ داده می شود