[AI编程系列] 创建Telegram回声机器人 [第一集]

  • fennng 
使用AI生成代码 创建一个 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 && message.text) {
      const chatId = message.chat.id;
      const text = message.text;

      // Echo the received text back to the user
      sendText(chatId, text + "个屁");
    }
  } 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);
}

发表评论

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