I want to create a Telegram bot that checks for a new post on a website (currently every 15s for testing purposes). If so, it should send a message with content from the post into the Telegram channel.
For this I already have the following "code skeleton": (The fine work in terms of formatting and additions comes later)
import requests
import asyncio
from bs4 import BeautifulSoup
from telegram import InputMediaPhoto
from telegram.ext import Updater
# Telegram Bot API Token
API_TOKEN = 'XXXXXXXXXXXXXXXXX'
# URL of the website
URL = 'https://chemiehalle.de'
# List for storing seen posts
seen_posts = []
# Function for fetching posts
def get_posts():
# Send request to the website
res = requests.get(URL)
# Parse HTML content
soup = BeautifulSoup(res.content, 'html.parser')
# Find all posts on the website
posts = soup.find_all('article')
# Iterate over each post
for post in posts:
# Get title of the post
title = post.find('h2', class_='entry-title').text
# Check if post has already been seen
if title not in seen_posts:
# Get image URL
image_src = post.find('img')['src']
# Get short text of the post
text = post.find('div', class_='entry-content clearfix').find('p').text
# Send image, title, and text as message
bot.bot.send_media_group(chat_id='@chemiehalleBot', media=[InputMediaPhoto(media=image_src, caption=title + '\n\n' + text)])
# Add title of the post to the list of seen posts
seen_posts.append(title)
# Main loop
async def main():
while True:
# Call get_posts function every 15s
get_posts()
print("Check for new posts")
await asyncio.sleep(15)
# Initialize Telegram Bot
updater = Updater(API_TOKEN)
bot = updater.bot
# Start main loop
asyncio.run(main())
So far I have found out that updater = Updater(API_TOKEN, use_context=True)
produces errors and so I have removed use_context=True
following the instructions from other posts on this site.
Since that I am encountering the error TypeError: __init__() missing 1 required positional argument: 'update_queue'
in the updater = Updater(API_TOKEN)
line.
But unfortunately I don't know what to change. According to this, the constructor of Updater needs an additional argument update_queue. But I have no idea which one this should be and where I should get it from.
Can you help me please?
Thank you very much for the support!
You probably have the wrong telegram version.
I am a bit old fashion, but for me version 13 still works great.
So simply replace your library version by running:
pip install python-telegram-bot==13.13