telegram创建机器人,接口调用机器人
生活随笔
收集整理的這篇文章主要介紹了
telegram创建机器人,接口调用机器人
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
【開(kāi)發(fā)云】年年都是折扣價(jià),不用四處薅羊毛
文章目錄
- 一、創(chuàng)建機(jī)器人
- 二、使用步驟
- 1.輸入創(chuàng)建機(jī)器人消息/newbot
- 2.機(jī)器人命令列表
- 3.查看我的機(jī)器人
- 4.php接口調(diào)用機(jī)器人
一、創(chuàng)建機(jī)器人
瀏覽器打開(kāi) https://t.me/botfather,進(jìn)入客戶端顯示如下圖
二、使用步驟
1.輸入創(chuàng)建機(jī)器人消息/newbot
以次輸入名稱和用戶即可創(chuàng)建成功
2.機(jī)器人命令列表
3.查看我的機(jī)器人
在消息輸入命令 回車即可
出現(xiàn)剛才創(chuàng)建的機(jī)器人,證明創(chuàng)建成功
4.php接口調(diào)用機(jī)器人
<?php/*** @desc Telegram機(jī)器人接口對(duì)接api* Class Tgapi*/ class Tgapi {protected $noNeedLogin = ['*'];protected $noNeedRight = ['*'];protected $token = null;protected $url = null;public function _initialize(){$this->token = "xxxxxxxxxx";//token設(shè)置機(jī)器人的TOKEN,申請(qǐng)機(jī)器人時(shí)獲取$this->url = 'https://api.telegram.org/bot' . $this->token . '/';//請(qǐng)求Telegram的URL}/*** @desc 設(shè)置更新hook地址,對(duì)機(jī)器人進(jìn)行回調(diào)Api地址的綁定,這樣用戶給你機(jī)器發(fā)消息就可以推送到自己服務(wù)器的接口上*/public function setWebHook(){$url = "https://***.com/api/tg/tgapi/processMessage";$info = $this->apiRequest('setWebhook', array('url' => isset($argv[1]) && $argv[1] == 'delete' ? '' : $url));echo $info; // 返回true為成功}/*** @desc 對(duì)機(jī)器人進(jìn)行回調(diào)Api-api/tg/tgapi/processMessage 機(jī)器人回調(diào)信息方法* @return mixed|string*/public function processMessage(){$content = file_get_contents("php://input");$update = json_decode($content, true);$message = isset($update["message"]) ? $update["message"] : $update["edited_message"];// process incoming message$message_id = $message['message_id'];$chat_id = $message['chat']['id'];if (isset($message['text'])) {// incoming text message$text = $message['text'];$ret = User::where(["uuid" => $text])->update(["chatid" => $chat_id]);if ($ret) {$url = $this->url . 'sendMessage';$res = $this->post(array('chat_id' => $chat_id,"text" => '綁定成功'), $url);return $res;}} else {$this->apiRequest("sendMessage", array('chat_id' => $chat_id,"text" => 'I understand only text messages'));}}public 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;$payload = json_encode($parameters); // header('Content-Type: application/json'); // header('Content-Length: ' . strlen($payload));echo $payload;return true;}/** 發(fā)送消息* **/public function sendMessage($msgtext, $chat_id, $parse_mode = "HTML"){$url = $this->url . 'sendMessage';return $this->post(array('parse_mode' => $parse_mode,'chat_id' => $chat_id,"text" => $msgtext), $url);}public 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_markupif (!is_numeric($val) && !is_string($val)) {$val = json_encode($val);}}$url = $this->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 $this->exec_curl_request($handle);}public 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 wrongsleep(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;}public 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($this->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 $this->exec_curl_request($handle);}public function post($data, $url){if (is_array($data)) {$data = http_build_query($data, null, '&');}$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_HEADER, 0);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);curl_setopt($curl, CURLOPT_POST, 1);curl_setopt($curl, CURLOPT_POSTFIELDS, $data);$data = curl_exec($curl);curl_close($curl);return $data;} }配置好token,設(shè)置webhook,發(fā)消息給機(jī)器人就可以回調(diào)到自己服務(wù)器了
總結(jié)
以上是生活随笔為你收集整理的telegram创建机器人,接口调用机器人的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 简体转繁体镜像克隆站群-自动缓存-自动伪
- 下一篇: 百度seo html,百度镜像网站: 怎