Skip to content

Instantly share code, notes, and snippets.

@ozero
Created February 8, 2022 06:20
Show Gist options
  • Save ozero/58d50cfb82e74678b1c0f79e0d1f7209 to your computer and use it in GitHub Desktop.
Save ozero/58d50cfb82e74678b1c0f79e0d1f7209 to your computer and use it in GitHub Desktop.
Telegram bot を AWS API gateway + Lambda でやってみた
/*
https://qiita.com/leokryslov/items/35f98e8e24e797441150
が、そのままでは
- `event.message.text` じゃなく `event.body.message.text` だよね
- API Gatewayが"Malformed Lambda proxy response"とか吐いてた
ということで動くまで直したやつ。
*/
const https = require('https');
const BOT_TOKEN = process.env.BOT_TOKEN
const URL = "https://api.telegram.org/bot" + BOT_TOKEN + "/sendMessage?";
exports.handler = (event, context, callback) => {
let response = {
'statusCode': 200,
'body': JSON.stringify(event)
}
var replyUrl = "";
let event_body = "";
try{
event_body = JSON.parse(event.body);
}catch(e){
event_body = event.body;
}
let message = event_body.message;
let text = 'Echo: ' + message.text;
let chatId = message.chat.id;
replyUrl = URL + 'text=' + text + '&chat_id=' + chatId;
https.get(replyUrl, (res) => {
console.log("Got response: " + res.statusCode);
response.statusCode = 200;
callback(null, response);
}).on('error', (e) => {
console.log("Got error: " + e.message);
response.statusCode = 502;
callback(Error(e));
})
};
@ozero
Copy link
Author

ozero commented Feb 8, 2022

動くことは確認できたのであとは telegraf.js でやる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment