This guide explains how to set up a webhook for your Telegram bot to handle PDF files and process them using a Flask application.
- Python 3.6 or higher
pip
package managerngrok
for exposing your local server to the internet- Telegram bot token from BotFather
- API token for securing the endpoint
-
Clone the repository:
git clone https://github.com/yourusername/colli_parser.git cd colli_parser
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables: Create a
.env
file in the project root with:TELEGRAM_TOKEN=your_telegram_bot_token API_TOKEN=your_api_token CHATGPT_API_TOKEN=your_chatgpt_api_token
-
Start ngrok:
ngrok http 7071
Copy the HTTPS URL provided by ngrok (e.g.,
https://xxxx-xx-xx-xxx-xx.ngrok.io
) -
Set up the webhook: Replace
YOUR_NGROK_URL
andYOUR_API_TOKEN
with your values:source webhook/.env && curl -X POST "https://api.telegram.org/bot${TELEGRAM_TOKEN}/setWebhook" \ -H "Content-Type: application/json" \ -d '{"url": "'${NGROK_URL}'/webhook?code='${FUNCTION_KEY}'", "secret_token": "'${API_TOKEN}'"}'
Local Setup Note: Remove authentication (i.e.,
?code=function_key
) when configuring the webhook locally.source webhook/.env && curl -X POST "https://api.telegram.org/bot${TELEGRAM_TOKEN}/setWebhook" \ -H "Content-Type: application/json" \ -d '{"url": "'${NGROK_URL}'/webhook", "secret_token": "'${API_TOKEN}'"}'
-
Start the Flask application:
python app.py
- Start a chat with your Telegram bot
- Send a PDF invoice file to the bot
- The bot will process the invoice and return extracted information
- Keep your
.env
file secure and never commit it to version control - Regularly rotate your API tokens
- The webhook endpoint is protected with the API token
colli_parser/
├── app.py # Main Flask application
├── api_client.py # ChatGPT API client
├── invoice_parser.py # PDF parsing logic
├── utils.py # Utility functions
├── config.py # Configuration
├── data/ # PDF storage
└── .env # Environment variables
Open the webhook
folder in VS Code and launch the debugger.
az login
az deployment group create --resource-group colli_parser --template-file infra/azure.bicep --parameters infra/colliparser_params.bicep.bicepparam
func azure functionapp publish colli-parser-func