Telegram Bot 2: OCR

Python + AWS Lambda + AWS API Gateway

Previous Article:
Telegram Bot 1: Stock Tracking

0. Demo

  1. Setup

- create AWS account

- talk to BotFather in telegram and create a bot. You will receive an authorization token. https://core.telegram.org/bots
- download the script and fill in the token in lambda_function.py
https://github.com/simonlaw101/tg_bot

2. Create a Lambda function

- set a longer timeout (Optional)

3. Add a Layer for external library dependencies (Optional)

- create a new folder named “python” & install the required external library inside

- select “python” folder and add to archive (e.g. layer.zip)

*Note that the library must be in the following structure (inside python folder), otherwise you will get import module error during runtime

AWS official example file structure for the Pillow library:

- go to your function

- layer is immutable
- upload new version for any changes

4. Deploy code to Lambda

- create a folder including all your source code files

- select all files and add to archive (e.g. lambda_function.zip)

- upload the .zip file to lambda

5. Create an API Gateway

6. Set Webhook

- place the following URL in the browser and hit enter

- you should receive the following response:

- to view the Webhook information:

Reference:

https://dev.to/nqcm/-building-a-telegram-bot-with-aws-api-gateway-and-aws-lambda-27fg

https://docs.aws.amazon.com/lambda/latest/dg/python-package.html

https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-compile

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store