AWS Lambda function that handles Telegram bot messages and interfaces with the backend service.
-
Create a Telegram bot and get the bot token from @BotFather
-
Deploy the sibyl-core CloudFormation stack first
- This stack exports the API endpoint that this service depends on
- The stack name should follow the pattern
sibyl-core-{env}where env is eitherdevorprod
-
Store the bot token in AWS Parameter Store:
aws ssm put-parameter \
--name "/sibyl/telegram/bot-token" \
--value "YOUR_BOT_TOKEN" \
--type "SecureString" \
--description "Telegram Bot Token for Sibyl"This step is required before deploying the stack. If you deploy without setting up the parameter, the deployment will fail with a clear error message telling you how to fix it.
- Create a virtual environment and install dependencies:
python -m venv venv
source venv/bin/activate # On Windows: .\venv\Scripts\activate
pip install -r requirements.txt- Configure AWS Parameter Store:
- Store the Telegram bot token securely in AWS Parameter Store at
/sibyl/telegram/bot-token - Ensure the Lambda function has appropriate IAM permissions to access the parameter
Store the Telegram bot token in AWS Parameter Store:
Using AWS CLI:
aws ssm put-parameter \
--name "/sibyl/telegram/bot-token" \
--value "YOUR_BOT_TOKEN" \
--type "SecureString" \
--description "Telegram Bot Token for Sibyl"Or using AWS Console:
-
Go to AWS Systems Manager
-
Navigate to Parameter Store
-
Click "Create parameter"
-
Set:
- Name:
/sibyl/telegram/bot-token - Type: SecureString
- Value: Your Telegram bot token
- Name:
-
Click "Create parameter"
-
Deploy to AWS Lambda:
- Create a new Lambda function
- Set Python 3.12 as the runtime
- Set
lambda_handler.lambda_handleras the handler - Configure environment variables if needed
- The function URL will be automatically configured by SAM
-
Install AWS SAM CLI following the official instructions
-
Build the SAM application:
sam build- Deploy the application:
# First-time deployment
sam deploy --guided
# Subsequent deployments
sam deployThe webhook is automatically configured during the first deployment using a Custom Resource in CloudFormation. You don't need to run any additional commands.
To verify the webhook setup:
curl https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getWebhookInfoIf you need to manually set up the webhook, you can use the provided script:
./setup_webhook.py --stack-name <your-stack-name>- IP validation for Telegram webhook requests
- Message length validation
- Secure token storage using AWS Parameter Store
- Request validation using Pydantic models
- Comprehensive error handling and logging
To add the backend API client:
- Generate the Python client from your OpenAPI specification
- Add the generated client to the project
- Update the
lambda_handlerfunction to use the client for backend communication
TODO: Add test cases for:
- IP validation
- Message validation
- Backend API integration
- Telegram API integration