下面这个代码,是我看视频峰哥telegram bot教学里学会的, 然后自己综合了一下峰哥的代码, 搞了一个会自动进退群打招呼,指定内容回复的机器人。 也尝试做了两个机器人

[@ssrubot 奶子鸭][1]         [@zhutibot 主题酱][2]

    function doPost(e){
          var body = JSON.parse(e.postData.contents);
          body.message.chat.id = body.message.chat.id + '';
         
          var payload = preparePayload(body);
          var data = {    
            "method": "post",
            "contentType": 'application/json',
            "payload": JSON.stringify(payload),
          }
           
          UrlFetchApp.fetch("https://api.telegram.org/bot934292303:AAFE-SL4sBRCuZ76D0POdcPUM1ZvnefCKRM/", data);
        }
         
        function getName(user) {
          var name = user.first_name;
          if (user.last_name) {
            name += " " + user.last_name;
          }
         
          return name;
        }
         
        function escapeMarkDown(toEscapeMsg) {
          var escapedMsg = toEscapeMsg
          .replace(/_/g, "\\_")
          .replace(/\*/g, "\\*")
          .replace(/\[/g, "\\[")
          .replace(/`/g, "\\`");
            return escapedMsg;
        }
        
        function getMentionName(user) {
          var username = user.username;
          var mentionName = "";
          var name = getName(user);
          if (!name) {
            name = "神秘人";
          }
         
          // if (!username) {
            mentionName = getMarkDownUserUrl(escapeMarkDown(name), user.id);
          // } else {
          //   mentionName = "[" + escapeMarkDown(name) + "](@" + escapeMarkDown(username) + ")";
          // }
         
          return mentionName;
        }
         
        function getMarkDownUserUrl(userName, userId) {
          return "[" + userName + "](tg://user?id=" + userId + ")";
        }
         
        function preparePayload(body){
          var payload;   
          if (body.message.new_chat_member) {
             payload = {
                "method": "sendMessage",
                "chat_id": body.message.chat.id,
                "parse_mode": "Markdown",
                "disable_web_page_preview": true,
              } 
               
              payload.text = "*亲亲你好鸭~*" + getMentionName(body.message.new_chat_member) + "\n*欢迎来喝*酸奶*";
        
          if (body.message.left_chat_member) {
             payload = {
                "method": "sendMessage",
                "chat_id": body.message.chat.id,
                "parse_mode": "Markdown",
                "disable_web_page_preview": true,
              } 
               
              payload.text = getMentionName(body.message.left_chat_member) + "滚犊子!\n要是有冤情,就去找[管理员爸爸](https://t.me/wasabi213)饶了你~";
        
              return payload;
          }
        
          body.message.text = body.message.text.toLowerCase();
          body.message.text = body.message.text.replace(/@ssrubot/g, '');
           
          var paras = body.message.text.trim().split(" ");
          // remove empty strings
          paras = paras.filter(function(para){
            if (para){
              return true;
            }
          });
        
          if (body.message.text){  
              payload = {
                "method": "sendMessage",
                "chat_id": body.message.chat.id,
                "parse_mode": "Markdown",
                "disable_web_page_preview": true,
              } 
        
            var str = body.message.text;
            var n = str.search("鸭子");
            if(n == -1)
            {
                n = str.search("奶子");
            }
            if(n>=0){
                payload.reply_to_message_id = body.message.message_id;
                payload.text = "干嘛鸭?";
            }
        
              if(body.message.text.indexOf("/tg") === 0){   
                payload.text = "[tg主题机器人](https://t.me/zhutibot)";        
                 return payload;
              }   
              if(body.message.text.indexOf("/ssr") === 0){       
                payload.text = "点击进入===>>>[酸奶官网](https://www.skyyogurt.gq)\n"
                    var inlineKeyboardMarkup = {};
                    inlineKeyboardMarkup.inline_keyboard = [];
                    var keyboardRow = [];
                    var keyboardButton1 = {
                        text: "酸奶2.0NSFW",
                        url: "https://t.me/ssruSSR"
                    };  
                    var keyboardButton2 = {
                        text: "酸奶3.0(禁黄)",
                        url: "https://t.me/mysuannai"
                    };  
                   
                    var keyboardRow2 = [];
                    var keyboardButton3 = {
                        text: "pharos lite",
                        url: "https://apps.apple.com/us/app/pharos-pro/id1456610173"
                    };
                    var keyboardButton4 = {
                        text: "potatso lite",
                        url: "https://apps.apple.com/us/app/potatso-lite/id1239860606"
                    };  
        
                    keyboardRow.push(keyboardButton1);
                    keyboardRow.push(keyboardButton2);
                    keyboardRow2.push(keyboardButton3);
                    keyboardRow2.push(keyboardButton4);
                    inlineKeyboardMarkup.inline_keyboard.push(keyboardRow);
                    inlineKeyboardMarkup.inline_keyboard.push(keyboardRow2);
                    payload.reply_markup = inlineKeyboardMarkup;
                   return payload;
              }
          }
          return payload
        }

    [1]: https://t.me/ssrubot

    [2]: https://t.me/zhutibot


最后修改:2020 年 05 月 24 日 09 : 02 PM
如果觉得站长太瘦,请加入Wasabi喂养计划吧~