potato 回声机器人

  • sauween 
const token = '123:abctoke';          //  这里键入你得token
const BASE_URL = 'https://api.rct2008.com:8443/' + token + '/';
const myID = token.slice(0, token.indexOf(':'))



function setWebhook() {   // 这里写入setWebhook 然后先run 一次这个函数 才能将机器人接入GAS 
    var setWebhook = {
        method: 'setWebhook',
        payload: {
            url: 'https://script.google.com/macros/s/hojpoicsihjnvsopalL-kjeg/exec' //这里键入你得GS 发布地址
        }
    }
    Logger.log(postPotato(setWebhook))
}




function doGet(e) {
    return HtmlService.createHtmlOutput("Hello World!! No, this link should be hidden!!!");
}
function doPost(e) {
    var bodys = JSON.parse(e.postData.contents);
    bodys.forEach(element => {
        if (element) {
            payload = preparePayload(element);
            toPotato(payload)
        }
    });
}
function toPotato(payload) {
    if (Array.isArray(payload)) {
        payloads = payload;
    } else {
        payloads = [payload];
    }
    Logger.log(payloads)
    for (var i = 0; i < payloads.length; i++) {
        payload = payloads[i];
        if (payload) {
            var res = postPotato(payload);
            Logger.log(res)
        }
    }
}
function postPotato(payload) {
    let url = BASE_URL + payload.method
    delete payload.method
    var data = {
        'contentType': 'application/json',
        "method": "post",
        "payload": JSON.stringify(payload)
    }
    var response = UrlFetchApp.fetch(url, data);
    var res = JSON.parse(response);
    return res;
}
function preparePayload(body) {
    if (body.message && body.message.text) {
        if (body.message.from.id == myID) {     //判断信息来源是自己得 然后返回空对象
            return null
        }
        let mes = body.message.text;
        var sendTextMessage = {
            method: 'sendTextMessage',
            chat_type: body.message.chat.type,
            chat_id: body.message.chat.id,
            text: mes,
            // markdown: true,
        }

        if (mes == '小掌门') {
            const hello = ['你好', 'hi']
            sendTextMessage.text = hello[Math.floor(Math.random() * hello.length)]
        } else {
            sendTextMessage.text = mes
        }
        return sendTextMessage
    }
}

项目代码我也上传了gitee 无需翻墙直接访问?

《potato 回声机器人》有4个想法

发表评论

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