Telegram bot not responding as expected with news headlines

I’m working on a Telegram bot to fetch COVID-19 news headlines. I’ve got a web scraping function that works fine on its own:

import requests
from bs4 import BeautifulSoup as soup

def fetch_news():
    page = requests.get('https://example-news.com/covid')
    content = soup(page.text, 'html.parser')
    titles = content.find_all('h3', class_='article-title')
    for title in titles:
        print(title.text.strip())

fetch_news()

This gives me the headlines I want. But when I try to use it in my bot code, it’s not working:

import telegram
from telegram.ext import Updater, MessageHandler, Filters

def fetch_news():
    # Same as above

bot = telegram.Bot('my-token')
updater = Updater('my-token', use_context=True)

def respond(update, context):
    if update.message.text == 'News':
        headlines = fetch_news()
        bot.send_message(chat_id=update.effective_chat.id, text=headlines)

updater.dispatcher.add_handler(MessageHandler(Filters.text, respond))
updater.start_polling()

I’m getting an error: ‘Message text is empty’. Should I change the fetch_news function or fix something in the bot’s response code? Any help would be great!

Your bot isn’t receiving any headlines because the fetch_news function only prints them instead of returning them. Instead of printing, try storing the headlines in a list, join them into a single string, and then return this string. That way, when the bot calls fetch_news, there will be content to send back in its message.

For example:

 def fetch_news():
     page = requests.get('https://example-news.com/covid')
     content = soup(page.text, 'html.parser')
     titles = content.find_all('h3', class_='article-title')
     headlines = [title.text.strip() for title in titles]
     return '\n'.join(headlines)

This adjustment should fix the ‘Message text is empty’ error by ensuring the bot actually gets data to output.

I’ve run into this exact issue before when building a news bot. The problem isn’t with your fetch_news function, but how you’re handling the data it produces. You’re printing the headlines instead of returning them, so your bot has nothing to send.

Here’s a quick fix that should work:

 def fetch_news():
     page = requests.get('https://example-news.com/covid')
     content = soup(page.text, 'html.parser')
     titles = content.find_all('h3', class_='article-title')
     headlines = [title.text.strip() for title in titles]
     return '\n'.join(headlines)

 def respond(update, context):
     if update.message.text == 'News':
         headlines = fetch_news()
         bot.send_message(chat_id=update.effective_chat.id, text=headlines)

This modification collects the headlines into a list, joins them with newlines, and returns them as a single string. Your bot should now be able to send the headlines properly. Let me know if you need any more help!

hey luke, looks lik your news function isnt returning data. try returnin the headlines a string rather than printin them so the message isnt empty. hope it helps.