A modern serverless Telegram bot template using Grammy, TypeScript, and AWS Lambda.
- Built with Grammy - a modern Telegram Bot API framework
- TypeScript for type safety and better developer experience
- Serverless deployment with AWS Lambda
- Internationalization support with @grammyjs/i18n
- Organized command structure
- Local development with hot reloading
- Node.js (v16 or newer)
- Yarn package manager
- AWS account (for deployment)
- Telegram Bot Token (get one from @BotFather)
-
Clone this repository:
git clone https://github.com/duckyb/sls-telegram-bot.git cd sls-telegram-bot
-
Install dependencies:
yarn install
-
Configure your bot:
- Edit
configuration.ts
and add your bot tokens - Update
serverless.yml
with your AWS settings if needed
- Edit
Run the bot locally:
yarn start
This will start the bot in development mode with hot reloading.
-
Deploy to AWS Lambda:
yarn deploy
-
After deployment, you'll get an endpoint URL. Copy this URL and update it in your
configuration.ts
file. -
Set the webhook:
yarn setHook
The template comes with a few example commands:
/start
- Welcome message/help
- Show help information
- Create a new handler in the
commands
directory:
// commands/mycommand.ts
import { MyContext } from '../types';
export const handleMyCommand = async (ctx: MyContext): Promise<void> => {
await ctx.reply('This is my new command!');
};
- Export it in
commands/index.ts
:
// Export your command handler
export { handleMyCommand } from './mycommand';
- Register it in
bot.ts
:
// Import your command handler
import { handleMyCommand } from './commands';
// Add this with other command handlers
bot.command('mycommand', handleMyCommand);
- Add it to the help message in
locales/en.ftl
- Modify the
locales/en.ftl
file to customize bot messages - Add more languages by creating additional
.ftl
files in thelocales
directory - Extend the
types.ts
file to add custom context types
If the webhook isn't working:
- Ensure the endpoint URL in
configuration.ts
is correct - Check AWS Lambda permissions
- Run
yarn delHook
followed byyarn setHook
to reset the webhook
The project uses strict TypeScript settings. When adding new features:
- Ensure proper typing for all variables and functions
- Extend the
MyContext
type intypes.ts
if adding new context properties
If deployment fails:
- Verify AWS credentials are set up correctly
- Check the
serverless.yml
configuration - Ensure all dependencies are properly installed
MIT