Telegram Bot 电报聊天机器人连接 RestHeart MongoDB 数据库

  • fennng 

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

相关的代码请到翻到下面

-~-~~-~~~-~~-~-

本频道旨在分享生活中各种小技巧, 如用小米盒子看国内视频, 如何使用KODI看电影等等. 同时我也会分享一些编程的教程, 如编写Telegram机器人,
建站等。 点击进入我的频道: 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
-~-~~-~~~-~~-~-

本集只提供新的 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;
}



《Telegram Bot 电报聊天机器人连接 RestHeart MongoDB 数据库》有4个想法

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注