• 欢迎订阅我的Youtube频道
  • 欢迎加入我的电报群

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

Youtube fennng 5个月前 (03-22) 745次浏览 2个评论

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

相关的代码请到翻到下面

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

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

🔷 订阅我的频道: goo.gl/KuF3bY
🔷 telegram 电报群: t.me/fengsharegroup
🔷 我的博客: http://www.dengnz.com/blog
🔷 Facebook: http://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;
}




峰享技术 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Telegram Bot 电报聊天机器人连接 RestHeart MongoDB 数据库
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(2)个小伙伴在吐槽
  1. 完善了delete的修改版:https://ideone.com/ZU9HgP
    eget2019-04-24 03:43 Reply