«

Telegram Bot 电报聊天机器人连接 RestHeart MongoDB 数据库【第十三集】

时间:2024-2-12 13:35     作者:7米网-域名管理专家     分类: 杂文分享




本视频是Telegram Bot 机器人编程系列的第十三集。 本集介绍如何使用机器人来连接 RestHeart Mongo DB.

本集只提供新的 Restheart.gs 文件, 其它文件请看之前的视频, 跳着看是不行地

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;

}




版权所有:7米网-域名管理专家
文章标题:Telegram Bot 电报聊天机器人连接 RestHeart MongoDB 数据库【第十三集】
除非注明,文章均为 7米网-域名管理专家 原创,转载请注明作者和出处 7米网-域名管理专家
取消
微信二维码
微信二维码
支付宝二维码