To create a simple Python Telegram bot that extracts and processes .txt files with specific links, and deploy it on Heroku, you can follow these steps. The bot will read .txt files sent to it, parse the links, and return them. This basic implementation will set the foundation for further enhancements based on your requirements.
Step 1: Preliminary Requirements
Python: Ensure you have Python 3.x installed on your system.
Telegram Bot Token: Create a bot via BotFather on Telegram and get your unique bot token.
Heroku Account: Sign up for a free account at Heroku.
GitHub Account: If you don't have one, create an account.
Step 2: Create the Bot Script
Install Required Packages:
First, install the python-telegram-bot library, which simplifies the interaction with the Telegram Bot API. Run this command in your terminal:
bash
pip install python-telegram-bot
Bot Script (bot.py):
Create a file named bot.py and paste the following code. This script initializes a simple bot that can accept text files, parse links within, and send them back.
python
from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
import os
import re
TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN_HERE'
def start(update: Update, context: CallbackContext) -> None:
update.message.reply_text('Hi! Send me a .txt file with links.')
def handle_text_file(update: Update, context: CallbackContext) -> None:
document = update.message.document
if document.mime_type == 'text/plain':
document.get_file().download(custom_path='tempfile.txt')
with open('tempfile.txt', 'r') as file:
content = file.read()
links = re.findall(r'https?://[^\s]+', content)
update.message.reply_text('\n'.join(links) if links else "No links found.")
else:
update.message.reply_text('Please send a .txt file.')
def main() -> None:
updater = Updater(TOKEN)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(MessageHandler(Filters.document.mime_type("text/plain"), handle_text_file))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
Replace 'YOUR_TELEGRAM_BOT_TOKEN_HERE' with your actual Telegram bot token.
Step 3: Prepare for Deployment
requirements.txt:
List all the Python packages that Heroku needs to install.
code
python-telegram-bot==13.7
Procfile:
Create a file named Procfile (without any extension) in the root directory of your project with the following content. This file tells Heroku how to run your bot.
code
worker: python bot.py
.gitignore:
Create a .gitignore file to tell Git which directories and files to ignore.
code
venv/
*.pyc
tempfile.txt
Step 4: Deploy on Heroku
Initialize a Git repository and commit your code:
bash
git init
git add .
git commit -m "Initial commit"
Create a new Heroku app and push your code:
Ensure you have Heroku CLI installed and logged in.
bash
heroku create
git push heroku master
Scale your bot:
By default, the app is not running. Scale it to one worker.
bash
heroku ps:scale worker=1
Your bot should now be running on Heroku. Send it a .txt file with links to see it in action.
Step 5: GitHub Repository
To create a GitHub repository, follow these steps on the GitHub website:
Log in to your GitHub account.
Click on the "+" icon in the upper right corner and select "New repository".
Fill out the form with your repository details and click "Create repository".
Follow the instructions shown to push your local repository to GitHub.
This guide gets you started, but remember to secure your bot token and consider adding more features or error handling as needed.