//关于如何部署可以查看峰哥教程第二集回声机器人的教程,代码里面自带了一个共享的数据库,如果想要自己的独有的数据库可以PY峰哥白嫖一个数据库或参考这篇文章https://url.cn/55FecZe自己建造
// bot token 将你机器人的token替换成自己ide var TOKEN = '1008091730:AAGye6loZhdwz_jJd7p4QA_3T_WYKxirHvI'; // my userid 这里是你的用户ID 私聊小掌门发送 /getid 就可以得到 var MyuserID = 1022285664 function doGet(e) { return HtmlService.createHtmlOutput("Hello World!! No, this link should be hidden!!!"); } // telegram api base url var BASE_URL = 'https://api.telegram.org/bot' + TOKEN + '/'; function doPost(e) { var body = JSON.parse(e.postData.contents); body.message.chat.id = body.message.chat.id + ''; mongo.insert("logs", body) var payload = preparePayload(body); 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 postTelegram(payload) { var data = { 'contentType': 'application/json', "method": "post", "payload": JSON.stringify(payload) } var response = UrlFetchApp.fetch(BASE_URL, data); var res = JSON.parse(response); // mongo.insert("logs", res) return res; } function preparePayload(body) { if (body.message) { if (body.message.chat.type == "private") { if (body.message.text == "/start") { var sendMeswho = { "method": "sendMessage", "chat_id": body.message.chat.id, "text": "好的你可以发消息联系我了", } return sendMeswho; } if (body.message.chat.id == MyuserID) { if (body.message.reply_to_message) { var sendMegme = { "method": "sendMessage", "text": body.message.text, } if (body.message.reply_to_message.forward_from) { sendMegme.chat_id= body.message.reply_to_message.forward_from.id return sendMegme } else { var data = { 'message.date' :body.message.reply_to_message.forward_date } data= mongo.get('logs',"filter="+JSON.stringify(data)) data = data._embedded[0].message.chat.id sendMegme.chat_id= data return sendMegme } } else { var sendMeswho = { "method": "sendMessage", "chat_id": body.message.chat.id, "text": "你需要回复那条消息?", } return sendMeswho; } } var Mesforme = { "method": "forwardMessage", "chat_id": MyuserID, "from_chat_id": body.message.from.id, "message_id": body.message.message_id, } return Mesforme } } } var mongo; var restheart = {}; var restheartUrl = "https://restheart.dengnz.com/user2/"; restheart.insert = function(collection, data) { var db = restheartUrl + collection; var option = getInsertOption(data); var response = UrlFetchApp.fetch(db, option); }; function testRestHeart(){ // restheart.replace("to-telegram", null, {desc: "record all message send to telegram chat"}); // restheart.replace("telegram-response", null, {desc: "record all message return from telegram"}); // restheart.replace("logs", null, {desc: "record some other logs"}); var data = {test: 'test2'}; restheart.insert("logs", data); // var data = {test: 'test replace'}; // restheart.replace("testColl/5c75be5e294610d3cbaa39f9", null, data); // var data = {test: 'test get'}; // restheart.get("testColl/5c75be5e294610d3cbaa39f9", null, data); // // var data = {test: 'test set'}; // restheart.set("testColl/5c75be5e294610d3cbaa39f9", null, data); // var data = {"$addToSet":{"array" : "bar2"}}; // restheart.setOne("testColl/5c75be5e294610d3cbaa39f9", data); } restheart.replace = function(collection, query, data) { var db = restheartUrl + collection; if (query){ db += "?" + query; } var option = getPutOption(data); var url = encodeURI(db); UrlFetchApp.fetch(url, option); }; restheart.setOne = function(urlWithId, data) { var db = restheartUrl + urlWithId; var option = getPatchOption(data); option.muteHttpExceptions = true; var url = encodeURI(db); var response = UrlFetchApp.fetch(encodeURI(db), option); } restheart.setMany = function(collection, query, data) { var db = restheartUrl + collection; if (query){ db += "/*?" + query; } var option = getPatchOption(data); var url = encodeURI(db); UrlFetchApp.fetch(url, option); } restheart.set = restheart.setMany; restheart.remove = function(collection, query) { var db = restheartUrl + collection; if (query){ db += "/*?" + query; } var option = getDeleteOption(); var url = encodeURI(db); UrlFetchApp.fetch(url, option); } restheart.get = function(collection, query) { var db = restheartUrl + collection; if (query){ db += "?" + query; } var option = getGetOption(); var response = UrlFetchApp.fetch(encodeURI(db), option); var contentText = response.getContentText(); var responseString = JSON.stringify(response); var object = JSON.parse(response); return object; } mongo = restheart; function getInsertOption(data){ var option = { "method": "post", 'contentType': 'application/json', 'headers': {"Authorization": "Basic " + Utilities.base64Encode('user2' + ":" + 'changeit')}, "muteHttpExceptions": true, "payload": JSON.stringify(data) }; return option; } function getDeleteOption(){ var option = { "method": "delete", 'contentType': 'application/json', 'headers': {"Authorization": "Basic " + Utilities.base64Encode('user2' + ":" + 'changeit')}, }; return option; } function getPutOption(data){ var option = { "method": "put", 'contentType': 'application/json', 'headers': {"Authorization": "Basic " + Utilities.base64Encode('user2' + ":" + 'changeit')}, "payload": JSON.stringify(data) }; return option; } function getPatchOption(data){ var option = { "method": "patch", 'contentType': 'application/json', 'headers': {"Authorization": "Basic " + Utilities.base64Encode('user2' + ":" + 'changeit')}, "payload": JSON.stringify(data) }; return option; } function getGetOption(){ var option = { "method": "get", 'headers': {"Authorization": "Basic " + Utilities.base64Encode('user2' + ":" + 'changeit')}, }; return option; }