# Telegram Bot 申請教學
###### tags: `telegram` `PHP`
## Telegram 簡介
Telegram 是近期竄升上來的通訊軟體,也可以自行開發程序,快速 輕便
## 如何申請 Bot
首先須申請一支telegram帳號 用電號號碼認證
搜尋 機器人之父(@Botfather),輸入 `/newbot` 申請好後會有一組token,這組token給api請求使用
## 發起API請求
所有api請求都是透過 `https://api.telegram.org/bot{$token}/<MethodName>`的方式,例如:`https://api.telegram.org/bot{$token}/getUpdates`
> MethodName 請參考 telegram api 文件
> https://core.telegram.org/bots/api#available-methods
## PHP 上如何發送 telegram 訊息
api需透過異步的方式去接收與發送機器人的訊息,在php上使用 `curl` 去取得api返回的資訊及透過 `curl` 去發送 `POST` 給api地址取回訊息
```php
$token = 'token';
$url = 'https://api.telegram.org/bot{$token}/sendMessage';
$chat_id = '接收使用者或者群組的ID';
$text = '';
$data = array(
'chat_id' => $chat_id,
'text' => $text,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
````