[AI编程系列] 使用Telegram机器人发送图片和按钮 [第三集]

  • fennng 
function sendMessage() {
  var token = '7625009398:AAENKZ1xbwYShbeDemHK0e1Bu41DYHh4oG8';
  var chatId = '-1002331072693';
  var message = '你好,世界';

  var url = 'https://api.telegram.org/bot' + token + '/sendMessage';

  var payload = {
    'chat_id': chatId,
    'text': message
  };

  var options = {
    'method': 'post',
    'contentType': 'application/json',
    'payload': JSON.stringify(payload)
  };

  UrlFetchApp.fetch(url, options);
}

const TOKEN = '7625009398:AAENKZ1xbwYShbeDemHK0e1Bu41DYHh4oG8';
const TELEGRAM_API_URL = `https://api.telegram.org/bot${TOKEN}`;

function doPost(e) {
  try {
    const update = JSON.parse(e.postData.contents);

    if (update.message) {
      const message = update.message;
      const chatId = message.chat.id;

      if (message.text) {
        const text = message.text;
        if (text === "/start") {
          sendPhotoWithButtons(chatId, "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3f/Walking_tiger_female.jpg/1200px-Walking_tiger_female.jpg", "欢迎使用本机器人");
        } else if (text.includes("你好")) {
          sendText(chatId, "你好,我是一个编程教学的演示机器人,非常高兴认识你。有什么问题,你可以直接问我的主人 @fennng");
        } else if (text.includes("在吗")) {
          sendText(chatId, "请不要问在不在,问就是不在,有事说事,收到后会马上回复您。");
        } else if (text.startsWith("/echo ")) {
          // Echo the received message back to the user
          sendText(chatId, JSON.stringify(update, null, 2));
        } else {
          // Echo the received text back to the user
          sendText(chatId, text + "个屁");
        }
      } else if (message.photo) {
        const fileId = message.photo[message.photo.length - 1].file_id;
        const caption = message.caption || "";
        sendPhoto(chatId, fileId, caption);
      } else if (message.document && message.document.mime_type === "video/mp4") {
        const fileId = message.document.file_id;
        const caption = message.caption || "";
        sendDocument(chatId, fileId, caption);
      } else if (message.animation) {
        const fileId = message.animation.file_id;
        const caption = message.caption || "";
        sendAnimation(chatId, fileId, caption);
      }
    } else if (update.callback_query) {
      handleCallbackQuery(update.callback_query);
    }
  } catch (error) {
    Logger.log('Error: ' + error.toString());
  }
}

function sendText(chatId, text) {
  const payload = {
    method: 'post',
    contentType: 'application/json',
    payload: JSON.stringify({
      chat_id: chatId,
      text: text
    })
  };

  const url = `${TELEGRAM_API_URL}/sendMessage`;
  UrlFetchApp.fetch(url, payload);
}

function sendPhoto(chatId, photoUrl, caption) {
  const payload = {
    method: 'post',
    contentType: 'application/json',
    payload: JSON.stringify({
      chat_id: chatId,
      photo: photoUrl,
      caption: caption
    })
  };

  const url = `${TELEGRAM_API_URL}/sendPhoto`;
  UrlFetchApp.fetch(url, payload);
}

function sendPhotoWithButtons(chatId, photoUrl, caption) {
  const payload = {
    method: 'post',
    contentType: 'application/json',
    payload: JSON.stringify({
      chat_id: chatId,
      photo: photoUrl,
      caption: caption,
      reply_markup: {
        inline_keyboard: [
          [{ text: '定制机器人找峰哥', url: 'https://t.me/fennng' }],
          [{ text: '帮助', callback_data: 'help' }, { text: '更多机器人', callback_data: 'more_bots' }]
        ]
      }
    })
  };

  const url = `${TELEGRAM_API_URL}/sendPhoto`;
  UrlFetchApp.fetch(url, payload);
}

function handleCallbackQuery(callbackQuery) {
  const chatId = callbackQuery.message.chat.id;
  const data = callbackQuery.data;

  if (data === 'help') {
    sendText(chatId, "欢迎使用本帮助系统,以下是本机器人的功能介绍, blabla");
  } else if (data === 'more_bots') {
    sendText(chatId, "以下是峰哥开发的其它机器人:\n\n@fengfaqbot\n@fengdoorbot\n@fengchatgptbot\n@fengdrawbot\n@fengstatsbot");
  }
}



function sendDocument(chatId, fileId, caption) {
  const payload = {
    method: 'post',
    contentType: 'application/json',
    payload: JSON.stringify({
      chat_id: chatId,
      document: fileId,
      caption: caption
    })
  };

  const url = `${TELEGRAM_API_URL}/sendDocument`;
  UrlFetchApp.fetch(url, payload);
}

function sendAnimation(chatId, fileId, caption) {
  const payload = {
    method: 'post',
    contentType: 'application/json',
    payload: JSON.stringify({
      chat_id: chatId,
      animation: fileId,
      caption: caption
    })
  };

  const url = `${TELEGRAM_API_URL}/sendAnimation`;
  UrlFetchApp.fetch(url, payload);
}

发表评论

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