[AI编程系列] 使用Telegram机器人处理特定的消息 [第二集]

  • fennng 

视频模糊的话,请手动调整质量为1080P,展开此处看代码 使用AI生成代码 让Telegram Telegram 机器人回复特定的指令 代码在这

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);
    const message = update.message;

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

      if (message.text) {
        const text = message.text;
        if (text.includes("你好")) {
          sendText(chatId, "你好,我是一个编程教学的演示机器人,非常高兴认识你。有什么问题,你可以直接问我的主人 @fennng");
        } else if (text.includes("在吗")) {
          sendText(chatId, "请不要问在不在,问就是不在,有事说事,收到后会马上回复您。");
        } 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);
      }
    }
  } 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, fileId, caption) {
  const payload = {
    method: 'post',
    contentType: 'application/json',
    payload: JSON.stringify({
      chat_id: chatId,
      photo: fileId,
      caption: caption
    })
  };

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

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

发表评论

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