【开发云】年年都是折扣价,不用四处薅羊毛 文章目录 一、创建机器人二、使用步骤1.输入创建机器人消息/newbot2.机器人命令列表3.查看我的机器人4.php接口调用机器人
浏览器打开 https://t.me/botfather,进入客户端显示如下图
以次输入名称和用户即可创建成功
在消息输入命令 回车即可
出现刚才创建的机器人,证明创建成功
token = "xxxxxxxxxx";//token设置机器人的TOKEN,申请机器人时获取 $this->url = 'https://api.telegram.org/bot' . $this->token . '/';//请求Telegram的URL } public function setWebHook() { $url = "https:/ 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; } 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_markup if (!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 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; } 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,设置WEBhook,发消息给机器人就可以回调到自己服务器了
来源地址:https://blog.csdn.net/fengdangxing/article/details/125222118
--结束END--
本文标题: telegram创建机器人,接口调用机器人
本文链接: https://lsjlt.com/news/389253.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0