- Open the Functions page on the lambda console
Creat funciton
->Author from scratch
.- Choose a name, e.g.
telegramBotTengeEuro
. - Runtime:
Python 3.8
. - Keep rest at default for now and press
Create fundtion
.
Reference: https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html
- Open the Functions page on the Lambda console.
- Choose the function.
- Under
Functional overview
, chooseAdd trigger
. - Select
API Gateway
. - For API, choose
Create an API
. - For API Type, choose
HTTP API
. - For Security, choose
Open
. - Choose
Add
. - Copy API endpoint for later use.
This code uses external modules: requests
and boto3
. boto3
is already supported by lambda runtime. When creating a lambda function in AWS that requires external modules, one of the options is to use a lambda layer that contains all these modules. That way, the lambda is independent of those modules, can be updated by itself, and also you can share that layer between lambdas that use the same modules, thus making it easier to maintain.
Layer with requests is included as lambda-layer.zip
. However, in case more external modules are needed, you can follow instruction below.
Before starting, make sure you're using python3.8 since lambda runtime is python3.8. First create a folder structure for the modules that need to be installed.
mkdir -p lambda-layer/python/lib/python3.8/site-packages
Once the folder is created we can install requests and any other module in that folder.
pip3 install requests --target lambda-layer/python/lib/python3.8/site-packages
That folder structure is important because that is where Python expects to find the modules.
Now we can go into the lambda-layer folder and create a zip file for the layer that will be uploaded using the console.
cd lambda-layer
zip -r9 lambda-layer.zip .
- Go to Services -> Lambda -> Layers and choose
Create layer
. - Name (ex: telegramBotRequestsLayer).
- Upload
lambda-layer.zip
. - For runtime choose Python 3.8 and choose
Create
.
- Go to Services -> Lambda -> Functions and select the lambda function.
- Under
Layers
chooseAdd a layer
. - Choose
Custom layers
and select the layer. - Press
Add
.
Reference: https://core.telegram.org/bots/api
- Talk to BotFather to create a new bot and get your bot token.
- Set webhook by sending following request with your
TELEGRAM_BOT_TOKEN
andLAMBDA_HTTP_API_ENDPOINT
(you can use browser): https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/setWebhook?url={LAMBDA_HTTP_API_ENDPOINT}
- Go to
Lambda
->Functions
and choose the lambda function. - Copy and paste code from
lambda_function.py
code source box and deploy. - In
Configuration
->Environment variables
set following values:
ADMIN_CHAT_ID
: admin's chat id (you can find this by texting the bot and printing the event)TELEGRAM_BOT_TOKEN
: bot token you got fron BotFatherCURRENCY_A
: name of currency A in english (e.g. tenge)CURRENCY_A_RUS
: name of currency A in russian (e.g. тенге)CURRENCY_B
: name of currency B in english (e.g. euro)CURRENCY_B_RUS
: name of currency B in russian (e.g. евро)
- In
Configuration
->General configuration
set memory to 256MB and timeout to 30secs.
Go to DynamoDB and create following tables:
- name:
{CURRENCY_A}2{CURRENCY_B}
(e.g.tenge2euro
); partition key name:user_id
, type: number; no sort key - name:
{CURRENCY_B}2{CURRENCY_A}
(e.g.euro2tenge
); partition key name:user_id
, type: number; no sort key - name:
{CURRENCY_A}2{CURRENCY_B}_telegram_hist
(e.g.tenge2euro_telegram_hist
); partition key name:user_id
, type: number; sort key name:date
, type: number - name:
{CURRENCY_A}2{CURRENCY_B}_transaction_history
(e.g.tenge2euro_transaction_history
); partition key name:date
, type: number; no sort key
Lambda need to access DynamoDB.
- Go to
IAM
->Roles
. - Choose default role used by the lambda.
- Choose
Add inline policy
. - For service choose
DynamoDB
. - Actions:
PutItem
,DeleteItem
,GetItem
,Scan
,Query
. - Resources:
All resources
. - Choose
Review policy
. - Set name to
DynamoDBReadWrite
.
Install pipenv
python3 -m pip install pipenv
Install from Pipfile
python3 -m pipenv install
Activate environment and run tests
python3 -m pipenv shell
python -m unittest test_lambda_function.py