Project template to easily create a Telegram bot as an AWS Lambda function.
The template uses serverless-offline and ngrok to make the development easy before you deploy it to AWS.
You need to contact the Telegram @BotFather to create your bot and get the token to use as an ENV here.
You can use a .env or
TELEGRAM_TOKEN=telegram_bot_token npm run devThis will start the serverless-offline and ngrok at the same port configured on PORT||3000 env.
After the server is up, you can send messagens directly through telegram to your local machine.
TELEGRAM_TOKENtoken from the @BotFatherDEBUGcomma separated namespaces to activate debug log
PORT||3000ngrok tunnel and serverless-offline port
On handler.js add a new command as a regex which even accepts named grouping as handlers parameters.
One important thing is handler order. The bot returns on the first handler match, so, if you have two different handlers to the same path, and one accepts parameters, put the one with parameters firts.
Simple command example:
// handler.js
const mycommand = require('./src/commands/mycommand')
...
bot.on(/\/mycommand/, mycommand) // mycommand handler should be a generator that yields messages
// to be sent back to telegram
// mycommand.js
async function* handler(message) {
yield `hi ${message.from.first_name} from my command`
}Named param command example:
// handler.js
const mycommand = require('./src/commands/mycommand')
...
bot.on(/\/mycommand (?<action>action1|action2) (?<opt>op1|op2)/, mycommand) //
// mycommand.js
async function* handler(message, { action, opt }[, match]) {// match is the regex.exec(text) return
yield `hi ${message.from.first_name} from my command. You want to do action ${action} with option ${opt}`
// match[0] === action, match[1] === opt
}Positional param command example:
// handler.js
const mycommand = require('./src/commands/mycommand')
...
bot.on(/\/mycommand (param1|param2) (opt1|opt2)/, mycommand) //
// mycommand.js
async function* handler(message, match) {// match is the regex.exec(text) return
yield `hi ${message.from.first_name} from my command. You want to do action ${match[0]} with option ${match[1]}`
}The deployment is already configured to use GitlabCI. If you don't use it, you can deploy using the serverless framework from your local machine.
Configure the required environment variables above and the following:
AWS_ACCESS_KEY_IDyour AWS service user Access Key IdAWS_SECRET_ACCESS_KEYyour AWS service user Secret Access Key
git push origin masterOne time configuration:
npm run configEvery deploy:
npm run deploy