【开发云】年年都是折扣价,不用四处薅羊毛

文章目录

  • 一、创建机器人
  • 二、使用步骤
    • 1.输入创建机器人消息/newbot
    • 2.机器人命令列表
    • 3.查看我的机器人
    • 4.php接口调用机器人

一、创建机器人

浏览器打开 https://t.me/botfather,进入客户端显示如下图

二、使用步骤

1.输入创建机器人消息/newbot


以次输入名称和用户即可创建成功

2.机器人命令列表

3.查看我的机器人


在消息输入命令 回车即可

出现刚才创建的机器人,证明创建成功

4.php接口调用机器人

<?php/*** @desc Telegram机器人接口对接api* Class Tgapi*/
class Tgapi
{protected $noNeedLogin = ['*'];protected $noNeedRight = ['*'];protected $token = null;protected $url = null;public function _initialize(){$this->token = "xxxxxxxxxx";//token设置机器人的TOKEN,申请机器人时获取$this->url = 'https://api.telegram.org/bot' . $this->token . '/';//请求Telegram的URL}/*** @desc 设置更新hook地址,对机器人进行回调Api地址的绑定,这样用户给你机器发消息就可以推送到自己服务器的接口上*/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 对机器人进行回调Api-api/tg/tgapi/processMessage 机器人回调信息方法* @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;}/** 发送消息* **/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,设置webhook,发消息给机器人就可以回调到自己服务器了


telegram创建机器人,接口调用机器人相关推荐

  1. java消息推送怎么实现_调用钉钉接口实现机器人推送消息

    一.摘要 现实交易中为了能及时了解发明者量化机器人交易状态,有时候我们需要将机器人所执行的交易结果发送到微信.邮箱.短信等等.但每天上百条各种各样的信息,使得对这些信息已经不敏感,导致重要的信息不能及 ...

  2. 百度UNIT 机器人多轮对话技能创建以及API调用

    百度UNIT 机器人多轮对话技能创建以及API调用 基于百度UNIT2.0 版本,实现简单的多轮人机对话功能 创建机器人必备条件 确定有哪些技能 例如:查询天气.订票.讲故事等,以畅越冰激凌套餐营销话 ...

  3. PHP-微信开发之图灵机器人--天气接口调用

    微信开发中,图灵机器人回复.天气.翻译.美食.地图.二维码功能,随处可见.大小网站.APP都在运用这项技术在项目中.今天简单介绍一下图灵机器人回复,天气接口调用.翻译接口,希望对朋友们有用. 效果案例 ...

  4. 淘宝客微信机器人SDK调用接口

    淘宝客微信机器人SDK调用接口定义 手机客户端上传的通知类消息 (删除了没用的) 8.手机客户端微信上线通知 9.手机客户端微信下线通知 10.微信个人号新增好友通知 11.微信个人号移除好友通知 1 ...

  5. 小白向 零基础创建并简单调用钉钉自定义机器人

    背景 鉴于钉钉最近的火爆,遭受广大学生"迫害"的钉钉一首[ 钉 钉 本 钉 ,在 线 求 饶 ]在B站火了起来 作为同是分期五星的用户,面对突如其来的求助被迫学习,简单调用钉钉自定 ...

  6. 聊天机器人(Ajax实现聊天机器人接口的调用)

    文章目录 前言 一.UI布局 二.用户内容渲染到聊天窗口 三.获取聊天机器人消息 1.jquery中的ajax 2.调用机器人接口 四.将机器人的聊天内容转化为语音播放 五.使用回车发送消息 总结 - ...

  7. 小程序接入「微信对话开放平台」,创建对话客服机器人

    当用户在小程序内发起对话,你是在后台抓紧安排人工回复吗? 由于等待回复时间过长,用户产生不满并弃用了你的小程序. "秒回"是对话的基本要求,及时回复才能给到用户尊重和被重视感.然而 ...

  8. 微信个人号机器人接口

    前段时间公司需求开发一套自定义的微信机器人,需求是可以自批量添加好友,自动聊天,自动回复,发朋友圈,转发语音,以及定时群发等,还可以提取聊天内容,进行数据汇总,what ????微信还可以这样做!!调 ...

  9. Python免费快速接入图灵机器人接口

    图灵机器人相信大家并不陌生.通过图灵机器人,开发者和厂商能够以高效的方式创建专属的聊天机器人.客服机器人.领域对话问答机器人.儿童/服务机器人等.下面给大家见到那介绍如何通过Python(2.7版本) ...

最新文章

  1. linux内核md5,浅谈MD5及简单使用
  2. 疯狂软件2月3日Android就业班课程详细
  3. xml getelementsbytagname php,用PHP编写和读取XML的几种方式
  4. 怎么样才显示暗=安装好了mysql_linux mysql安装
  5. java web宿舍管理系统源码_JavaWeb宿舍管理系统(附 演示、源码下载地址)
  6. AcWing 861. 二分图的最大匹配(匈牙利算法)
  7. Pytorch squeeze() 和 unsqueeze() 方法区别
  8. Atitit 部署了个webdav服务 as root 目录 1.1. WEB-INF copy to root dir only a web.xml use... 1 1.2. Java.ba
  9. DLAN 连接和使用体验——Twonky
  10. 10负3次方计算机语言,10的负3次方等于多少?
  11. Flash Video DRM 视频版权保护技术
  12. 137、易燃固体的分级
  13. supervised contrastive learning 解读
  14. 怎么样才算软件做得好?
  15. Ubuntu18.04实装autoware1.14.0(ROS Melodic、QT5.9.5、BagRunning)
  16. 2013 CVPR点评
  17. 人教版初中计算机教案全本,人教版初中信息技术教案全集
  18. 解密:LL与LR解析 1(译)
  19. 网站访客QQ获取系统
  20. Python3 使用科大讯飞 API 接口实现音频文件转写

热门文章

  1. 【企业信息化】第3集 免费开源ERP: Odoo 16 POS终端管理系统
  2. 第9章 开发实例-文件保险箱
  3. html 网页播放音乐
  4. 一名基金运营人的“日常”和“非常”
  5. 第三周--AEM汇编语言编程入门
  6. oracle中姓名取姓氏,数据清洗初记(12):姓与名的提取
  7. 黑马Redis视频教程实战篇(五)
  8. python 输出纯音频_python处理音频文件(mp3)
  9. 海康NVR中INVITE请求一直返回 400 Bad Request
  10. 实用小工具-----python3 pdf2docx轻松搞定pdf转word