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

Telegram Bot 发送漂亮的消息,粗体,斜体, 表格,按钮[第五集]

Youtube fennng 11个月前 (09-29) 2496次浏览 0个评论

本视频是 Telegram Bot 机器人编程系列的第五集。 本集介绍如何使用 Telegram 机器人发送漂亮的消息,包据粗体,斜体,链接,按钮,代码,表格等。

相关的代码请翻到下面

下期预告:
Telegram Bot 判断命令的来源和处理有人入群, 有人被踢等[第六集]

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

本频道旨在分享生活中各种小技巧, 如用小米盒子看国内视频, 如何使用 KODI 看电影等等. 点击进入我的频道: 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
-~-~~-~~~-~~-~-

本视频中使用的代码:

function doGet(e){
  return HtmlService.createHtmlOutput("Hello World!! No, this link should be hidden!!!");
  
 //  return HtmlService.createHtmlOutputFromFile('Index');
}


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/bot682267360:AAHmjSil8oylavD2pENLLpcMU1svaD7mVeA/", data);
}
function preparePayload(body){
  var payload;

  body.message.text = body.message.text.toLowerCase();
  body.message.text = body.message.text.replace(/@temptestbot2/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,
        "text": "你好, 欢迎使用本机器人, 本机器人现在只认识颜色。",
        "parse_mode": "Markdown",
        "disable_web_page_preview": true,
      } 
      
      if(body.message.text.indexOf("/help") === 0){      
         payload.text = "你好, 欢迎使用本机器人, 本机器人现在只认识颜色。";
         return payload;
      }
      
      if(body.message.text.indexOf("/colors") === 0){   
         payload.text = "红\n 黄\n 蓝";        
         return payload;
      }
      
      if(body.message.text.indexOf("/list") === 0){ 
         if (paras[1]){
           switch (paras[1].toLowerCase()){
             case "people":  
               if (paras[2]){
                 if ("JS 神技能".toLowerCase().indexOf(paras[2]) >= 0){
                   payload.text = "JS 神技能 - https://www.youtube.com/channel/UC6tPP3jOTKgjqfDgqMsaG4g";
                 }
                 if ("悟空的日常".toLowerCase().indexOf(paras[2]) >= 0){
                   payload.text = "悟空的日常 - https://www.youtube.com/channel/UCii04BCvYIdQvshrdNDAcww";
                 }
                 if ("YuFeng Deng".toLowerCase().indexOf(paras[2]) >= 0){
                   payload.text = "YuFeng Deng - https://www.youtube.com/channel/UCG6xoef2xU86hnrCsS5m5Cw";
                 }
               } else {                 
                 payload.text = "JS 神技能\n"
                   + "悟空的日常\n"
                   + "YuFeng Deng\n";        
                   return payload;
                }
               break;
             default:  
               payload.text = "红\n 黄\n 蓝";            
             break;
           }
                
           return payload;
         } else {         
           payload.text = "*JS 神技能*\n"
             + "[悟空的日常](https://www.youtube.com/channel/UCii04BCvYIdQvshrdNDAcww)\n"
             + "[*YuFeng Deng*](https://www.youtube.com/channel/UCG6xoef2xU86hnrCsS5m5Cw)\n"
             + "_YuFeng Deng_\n"
             + "`01|" + "UCii04BCvYIdQvshrdNDAcww" + " | `\n"
             + "`02|" + "UCG6xoef2xU86hnrCsS5m5Cw" + " | `\n"
             + "```javascript\n"
             + 'payload = {\n'
             + '    "method": "sendMessage",\n'
             + '    "chat_id": body.message.chat.id,\n'
             + '    "text": body.message.text,\n'
             + '}'
             + "```"
             ;
                       
            var inlineKeyboardMarkup = {};
            inlineKeyboardMarkup.inline_keyboard = [];
            var keyboardRow = [];
            var keyboardButton1 = {
                text: "按钮 1",
                url: "https://www.google.com"
            };
            
            var keyboardButton2 = {
                text: "按钮 2",
                url: "https://www.google.com"
            };
            
            var keyboardRow2 = [];
            var keyboardButton3 = {
                text: "按钮 3",
                url: "https://www.google.com"
            };
            
            var keyboardButton4 = {
                text: "按钮 4",
                url: "https://www.google.com"
            };

            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;
         }
      }
      

  
      payload = {
          "method": "sendMessage",
          "chat_id": body.message.chat.id,
          "text": body.message.text,
      } 
	
  }
  else if (body.message.sticker){
    payload = {
      "method": "sendSticker",
      "chat_id": body.message.chat.id,
      "sticker": body.message.sticker.file_id
    }
   }
  else if (body.message.photo){
    array = body.message.photo;
    text = array[1];
    payload = {
      "method": "sendPhoto",
      "chat_id": body.message.chat.id,
      "photo": text.file_id
    }
   }
    else {
    payload = {
      "method": "sendMessage",
      "chat_id": body.message.chat.id,
      "text": "Try other stuff"
    }
   }
  return payload
}



峰享技术 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Telegram Bot 发送漂亮的消息,粗体,斜体, 表格,按钮[第五集]
喜欢 (1)
发表我的评论
取消评论

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

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址