μλ νμΈμ κ³°μ΄μμ!
μ΄μ λ μΉ ν κ³Ό ν΄λ§μ λν΄μ κ°λ μ μ 리νλ ν¬μ€ν μ μ¬λ Έμλλ°μ!
μ€λμ μΉ ν λ°©μμΌλ‘ ν λ κ·Έλ¨ λ΄κ³Ό ν΅μ νλ λ²μ λν΄μ μμ보λλ‘ νκ² μ΅λλ€.
μ€λΉν΄μΌ νλ κ²
php μ€μΉκ° λμ΄μλ μΉμλ²
μΉ ν μ°κ²°μ μν callback url (λλ©μΈ μ°κ²°λ μ¬μ΄νΈκ° νμ) 1λ²κ³Ό μ°κ²°λ λλ©μΈμ΄ μ¬μΌ ν©λλ€.
μ λ NASκ° μμ΄μ μΉμλ²λΌλμ§ callback urlμ μ½κ² λ§λ€ μ μμλλ°μ
μμ μ€λΉλ¬Όμ΄ μ€λΉκ° λμμΌλ μ λ test_webhook.phpλΌλ νμΌμ λ§λ€μ΄μ£Όκ² μ΅λλ€.
https://μμ μ λλ©μΈ/test_webhook.php μ΄λ° μμΌλ‘ μ κ·Όμ΄ λκ² λμ.
php μμ€λ μλ μ μ κΈλ‘ νκΈ°νκ² μ΅λλ€. νμνμ λΆμ νΌμ³μ μ°Έκ³ ν΄μ£ΌμΈμ ^^
<?
/**
* @comment
* - ν
λ κ·Έλ¨ μΉν
λ°©μ ν΅μ
* @modified by κ°λ°μ½κ²νμκ³°
* @modified 22.11.01
**/
//λ΄ api ν ν°
define('BOT_TOKEN', 'μμ μ λ΄ ν ν°');
//api url
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
//μΉν
μ£Όμ
define('WEBHOOK_URL', API_URL.'setWebhook?url=μμ μ λλ©μΈ/test_webhook.php/'.BOT_TOKEN);
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 successful: {$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 (!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);
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_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($parameters));
curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
return exec_curl_request($handle);
}
function processMessage($message)
{
// process incoming message
if(is_array($message["message"]) == true)
{
$message_id = $message["message"]['message_id'];
$chat_id = $message["message"]['chat']['id'];
}
else if(is_array($message["channel_post"]) == true)
{
$message_id = $message["channel_post"]['message_id'];
$chat_id = $message["channel_post"]['chat']['id'];
}
if(isset($message["message"]['text']) || isset($message["channel_post"]['text']))
{
// incoming text message
if(is_array($message["message"]) == true)
{
$text = $message["message"]['text'];
}
else if(is_array($message["channel_post"]) == true)
{
$text = $message["channel_post"]['text'];
}
if(strpos($text, "/start") === 0)
{
apiRequestJson("sendMessage", array('chat_id' => $chat_id, "text" => 'Hello', 'reply_markup' => array(
'keyboard' => array(array('Hello', 'Hi')),
'one_time_keyboard' => true,
'resize_keyboard' => true)));
}
else if ($text === "Hello" || $text === "Hi")
{
apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'Nice to meet you'));
}
else
{
apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'Cool'));
}
}
else
{
apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'No.'));
}
}
if (php_sapi_name() == 'cli')
{
// if run from console, set or delete webhook
apiRequest('setWebhook', array('url' => isset($argv[1]) && $argv[1] == 'delete' ? '' : WEBHOOK_URL));
exit;
}
$content = @file_get_contents("php://input");
$update = json_decode($content, true);
if(!$update)
{
exit;
}
if(isset($update))
{
processMessage($update);
}
exit;
?>
μμ²λΌ php νμΌμ λ§λ€μ΄μ£Όκ³ λ΄ ν ν°κ³Ό κ°μ₯ μ€μν webhook μ€μ μΈλ°μ.
μΉ ν μ€μ urlμ
https://api.telegram.org/bot{λ΄ν ν°}/setWebhook?url={μμ μλλ©μΈ}/bot_webhook.php/{λ΄ν ν°}
μ΄λ° μμΌλ‘ μ€μ ν μ μμ΅λλ€.
μ μ λ΄ ν ν°μ 5695678451:AAEBBcGX3ZWCQG248E2Reo1x6HKqlc-IiDM μ λλ€.
λμ μ νλ©΄
https://api.telegram.org/bot5695678451:AAEBBcGX3ZWCQG248E2Reo1x6HKqlc-IiDM/setWebhook?url={μμ μλλ©μΈ}/bot_webhook.php/5695678451:AAEBBcGX3ZWCQG248E2Reo1x6HKqlc-IiDM
μ΄λ κ² λκ² λ€μ μμ μ λλ©μΈμ΄λΌλ κ³³μλ μκΉ μμμ λ§μλλ Έλ€μνΌ php νμΌκ³Ό μ°κ²°λ λλ©μΈμ΄μ΄μΌ ν©λλ€.
κ·ΈλΌ μΉ ν μ μ€μ ν΄λ³΄κ² μ΅λλ€.
μ μμ μΌλ‘ μΉ ν μ΄ μ€μ λμλ€κ³ ν©λλ€.
μ΄μ λΆν° μ΄ λ΄μ μ μ λνλ₯Ό ν μ μλ λ΄μ΄ λμμ΅λλ€.
function processMessage($message)
{
// process incoming message
if(is_array($message["message"]) == true)
{
$message_id = $message["message"]['message_id'];
$chat_id = $message["message"]['chat']['id'];
}
else if(is_array($message["channel_post"]) == true)
{
$message_id = $message["channel_post"]['message_id'];
$chat_id = $message["channel_post"]['chat']['id'];
}
if(isset($message["message"]['text']) || isset($message["channel_post"]['text']))
{
// incoming text message
if(is_array($message["message"]) == true)
{
$text = $message["message"]['text'];
}
else if(is_array($message["channel_post"]) == true)
{
$text = $message["channel_post"]['text'];
}
if(strpos($text, "/start") === 0)
{
apiRequestJson("sendMessage", array('chat_id' => $chat_id, "text" => 'Hello', 'reply_markup' => array(
'keyboard' => array(array('Hello', 'Hi')),
'one_time_keyboard' => true,
'resize_keyboard' => true)));
}
else if ($text === "Hello" || $text === "Hi")
{
apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'Nice to meet you'));
}
else
{
apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'Cool'));
}
}
else
{
apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'No.'));
}
}
μ μ μμ€ μ€μμ processMessageλΌλ ν¨μκ° μμμ΅λλ€.
μ¬κΈ°μ μ κ° μ λ ₯ν λ§μ λ°μμ μ΄λ»κ² λ΅μ ν΄μ€μΌ νλμ§ μ νμλλ°μ.
μ λ μ°μ /startλΌλ λͺ λ Ήμ΄λ₯Ό μ λ ₯νλ©΄
λ΄μ hello νλ©΄μ μ μκ² μΈλΌμΈ λ²νΌ λ κ°λ₯Ό μ£Όκ² λ©λλ€.
Helloμ HiλΌλ κ°μΌλ‘μ λμΌλ‘ μ§μ 보μμ£ .
μ΄λμ?? νμ€ν μ€μκ°μΌλ‘ λ°μμ ν©λλ€.
κ·ΈλΌ μ κ° HelloλΌλ λ²νΌμ ν΄λ¦ν΄ λ³Όκ²μ. κ·ΈλΌ λ΄μ Nice to meet youλΌλ κ°μ μ£Όλλ‘ μ€μ λμ΄μμ΅λλ€.
μ΄λ° μμΌλ‘ λ°λ‘λ°λ‘ μ κ° λ΄μκ² μ λ ₯λ°μ μ μλ κ°μ μ£Όμμ λ λ΄μ μ μκ² μ€μ λ κ°μΌλ‘ μλ΅νκ² λ©λλ€.
κ°μ μλ΅μ λͺ» λ°κ² μ€μ νλ©΄ Coolμ΄λΌλ κ°μ μ μκ² μ£Όκ² λ©λλ€.
μ΄μ μ¬κΈ°μ μ¬νκ³Όμ μ λνκ² λλ€λ©΄ λΉλ‘μ νμ©μ± λκ³ λ©μ§ λ΄μ΄ νμνκ² μ£ γ γ
μλλ μ κ° μ¬μ¬ν΄μ λ§λ€κ³ μλ λ μ¨ λ΄μ λλ€.
κ·Έλ°μλ λΉνΈμ½μΈ μμΈ μλ¦Ό λ΄λ μμ΄μ γ γ μμ§ νμ°Έ λ¬Όλ €μμ΄μ γ γ
μ€μ ν κ°κ²©λμ λλ¬νλ©΄ 1λΆλ§λ€ μλ¦Όμ΄ κ³μ μ€κ²λ λμ΄μμ΅λλ€.
μΉ ν μ μ¨λ³΄μλλ°μ μ΄μ μ μ λ΄ ν ν°μ μμ ν μμ μ λλ€.
μλ λ΄ ν ν°μ μ μΆλλ©΄ μ λκΈ°μ ν¬μ€ν μ μν΄ μ κΉ λμ λ§λ κ±°λΌμμ γ γ
μ΄μ ν¬μ€ν μ λ§μΉλλ‘ νκ² μ΅λλ€ κΈ΄ κΈ μ½μ΄μ£Όμ μ κ°μ¬ν©λλ€!
'κ°λ°κ΄λ ¨ > PHP' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[PHP] ν λ κ·Έλ¨ λ μ¨ λ΄ (7) | 2022.11.13 |
---|---|
[PHP] ν λ κ·Έλ¨ μΉν (WebHook) κ³Ό ν΄λ§(Polling) λ°©μμ μ°¨μ΄μ #3 (19) | 2022.10.31 |
[PHP] ν λ κ·Έλ¨ λ΄ λ©μΈμ§ 보λ΄κΈ° #2 (16) | 2022.10.28 |
[PHP] ν λ κ·Έλ¨ λ΄ λ§λ€κΈ° #1 (6) | 2022.10.27 |
[PHP] μμμ κ°λ κ³Ό μ¬μ©λ² (0) | 2022.08.04 |
[PHP] λ¬Έμμ΄μ λ°°μ΄λ‘ λ°°μ΄μ λ¬Έμμ΄λ‘ explode/implode ν¨μ μ¬μ©λ²κ³Ό μμ (0) | 2022.08.03 |
[PHP] λ³μκ° λ°°μ΄μΈμ§ μλμ§ νμΈνλ ν¨μ is_array() μ¬μ©λ² λ° μμ (0) | 2022.08.03 |
[PHP] λ°°μ΄μμ κ°μ νμΈν΄μ£Όλ ν¨μ in_array() μ¬μ©λ² λ° μμ (0) | 2022.08.03 |
λκΈ