Telegram Bot 2: OCR

Simon Law
4 min readJan 10, 2021

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.
- download the script and fill in the token in

token = 'YOUR_TOKEN'

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

pip3 install --target . YOUR_LIBRARY

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

*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:
│ python/PIL
└ python/Pillow-5.3.0.dist-info

- 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.

- upload the .zip file to lambda

5. Create an API Gateway

6. Set Webhook

- place the following URL in the browser and hit enter<YOUR_TOKEN>/setWebHook?url=<INVOKE_URL>

- you should receive the following response:

{"ok":true,"result":true,"description":"Webhook was set"}

- to view the Webhook information:<YOUR_TOKEN>/getWebhookInforesponse: