Created
February 8, 2022 06:20
-
-
Save ozero/58d50cfb82e74678b1c0f79e0d1f7209 to your computer and use it in GitHub Desktop.
Telegram bot を AWS API gateway + Lambda でやってみた
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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)); | |
}) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
動くことは確認できたのであとは telegraf.js でやる。