Tags: Telegram, 机器人, 聊天机器人,机器人编程,机器人代码, Telegram代码,Telegram Bot, Hello world, Telegram编程 , AI
通过100多行代码将Telegram 机器人接到国产的AI 大脑, 功能强大, 懂数学, 懂成语,懂天气, 会讲笑话,还能玩游戏。
机器人的代码请翻到后面
代码已经写好,只要小小的改动就可以使用。
-~-~~-~~~-~~-~-
本频道旨在分享生活中各种小技巧, 如用小米盒子看国内视频, 如何使用KODI看电影等等. 点击进入我的频道: goo.gl/5tyxcf
🔷 订阅我的频道: goo.gl/KuF3bY
🔷 telegram电报群: t.me/fengsharegroup
🔷 我的博客: www.dengnz.com/blog
🔷 Facebook: www.facebook.com/fengnz
🔷 Twitter: https://twitter.com/fennng
觉得我的视频对你帮助很大的话, 请我喝杯咖啡吧
微信赞赏码: goo.gl/uKoE8w
-~-~~-~~~-~~-~-
[javascript]
function doPost(e) {
var body = JSON.parse(e.postData.contents);
if (!checkIsMyMessage(body)){
return;
}
if (body.message && body.message.chat){
}
var payload = tulinBotPayload(body);
var payloads;
if (Array.isArray(payload)){
payloads = payload;
} else {
payloads = [payload]
}
for (var i = 0; i < payloads.length; i++){
payload = payloads[i];
if (payload){
var res = PostTelegram(payload);
}
}
}
function tulinBotPayload(body) {
var payload;
var chatId = body.message.chat.id;
payload = {
"method": "sendMessage",
"chat_id": chatId,
"text": "",
"disable_web_page_preview": true,
"reply_to_message_id": body.message.message_id,
};
var tulinReq = {
"reqType": 0,
"perception": {
"inputText": {
// tulinjiqirenbot 改成你自己机器人的名字
"text": body.message.text.replace("@tulinjiqirenBot", "")
}
},
"userInfo": {
// 改成你自己的图灵机器人的apikey 和 userId
"apiKey": "4e8395e7721046e38e9c610de725edd7",
"userId": "339657"
}
};
var data = {
'contentType': 'application/json',
"method": "post",
"payload": JSON.stringify(tulinReq)
};
var response = UrlFetchApp.fetch("http://openapi.tuling123.com/openapi/api/v2", data);
var res = JSON.parse(response);
if (res.results && res.results[0].values && res.results[0].values.text) {
payload.text = res.results[0].values.text;
}
return payload;
}
function isPrivate(body) {
var share = getShareObject(body);
var chat = share.chat;
if (chat.type === "private") {
return true;
}
return false;
}
function PostTelegram(payload) {
var data = {
'contentType': 'application/json',
"method": "post",
"payload": JSON.stringify(payload)
};
// 改成你自己机器人的 API token
var response = UrlFetchApp.fetch("https://api.telegram.org/bot750371992:AAGTJC08Gg3g9I-gVm6BkAb30wsMVyQMbUI/", data);
var res = JSON.parse(response);
return res;
}
function checkIsMyMessage(body){
var text = body.message.text;
if (isPrivate(body)){
return true;
}
if (body.message.reply_to_message) {
if (body.message.reply_to_message.from.id == 750371992) {
return true;
}
}
if (text) {
var paras = text.split(" ");
if (paras[0] && paras[0].indexOf("@") >= 0){
// tulinjiqirenbot 改成你自己机器人的名字, 全部小写
if (paras[0].toLowerCase().indexOf("tulinjiqirenbot") >= 0){
return true;
}
return false;
}
}
return false;
}
function getShareObject(body) {
var payload;
var from;
var chat;
var messageId;
var callback_query_id;
// use editMessage method instead if it's a callback
if (body.callback_query) {
chat = body.callback_query.message.chat;
messageId = body.callback_query.message.message_id;
from = body.callback_query.from;
callback_query_id = body.callback_query.id;
payload = {
"method": "editMessageText",
"chat_id": chat.id,
"message_id": messageId,
"text": "",
"parse_mode": "markdown",
"disable_web_page_preview": false,
};
} else {
from = body.message.from;
chat = body.message.chat;
messageId = body.message.message_id;
payload = {
"method": "sendMessage",
"chat_id": chat.id,
"text": "",
"parse_mode": "markdown",
"disable_web_page_preview": true,
"reply_to_message_id": messageId,
};
}
var share = {};
share.payload = payload;
share.from = from;
share.chat = chat;
share.messageId = messageId;
share.callback_query_id = callback_query_id;
return share;
}
[/javascript]
没有评论:
发表评论