1. Trang chủ
  2. Blog
  3. Script PHP
  4. PHP tạo chat bot telegram đơn giản
Script PHP

PHP tạo chat bot telegram đơn giản

PHP tạo chat bot telegram đơn giản

Tạo BOT từ telegram

  1. Search bot BotFather
  2. Gõ lệnh /newbot
  3. Lấy bot group ID nhanh bằng cách add @RawDataBot


Kết quả

class TelegramModel
{
public static function send($content)
{
$content = is_array($content) && !empty($content) ? implode(PHP_EOL, $content) : $content;

$token = "5160686247:AAGUQsYeTtWD9ZNGUHxTjIc";
$chatID = "-574816132";

$url = "https://api.telegram.org/bot" . $token . "/sendMessage?chat_id=" . $chatID;
$url = $url . "&text=" . urlencode($content);
$ch = curl_init();
$optArray = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true
);
curl_setopt_array($ch, $optArray);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}

Sau cùng gắn vào chổ cần thông báo. Ví dụ thông báo đăng nhập.

$messageTelegram[] = '<strong>Thông báo đăng nhập</strong>';
$messageTelegram[] = '<strong> - Name</strong>: ' . $myUser->fullname;
$messageTelegram[] = '<strong> - Email</strong>: ' . $myUser->email;
$messageTelegram[] = '<strong> - Username</strong>: ' . $myUser->username;
$messageTelegram[] = '<strong> - Time</strong>: ' . date("Y/m/d H:i A");
$messageTelegram[] = '<strong> - Agent</strong>: ' . $agent;
$messageTelegram[] = '<strong> - IP</strong>: ' . $this->input->ip_address();
$messageTelegram[] = '<strong> - Platform</strong>: ' . $this->agent->platform();
$messageTelegram[] = '<strong> - Version</strong>: ' . $this->agent->version();

TelegramModel::send($messageTelegram);

Tạo BOT từ telegram


Kết quả

Tạo BOT từ telegram image widget

Văn Tình
Văn Tình
Full-Stack Developer

Đam mê code, thích du lịch bụi, lúc rãnh viết blog chia sẽ một vài thứ hay đã từng gặp.