Telegram Bot Not Returning File Content in Response

Hey everyone! I’m trying to make a Telegram bot that checks a website’s status and also returns some info from a local file. But I’m having trouble with the file part. Here’s what I’ve got so far:

import telebot
import requests

bot = telebot.TeleBot('MY_BOT_TOKEN')
site_url = 'http://example.com'

@bot.message_handler(content_types=['text'])
def handle_messages(msg):
    if msg.text.lower() == 'status':
        bot.reply_to(msg, check_status())

def read_status_file():
    with open('status_info.txt') as f:
        return f.readline().strip()

def check_status():
    site_response = requests.get(site_url)
    status = 'Up' if site_response.status_code == 200 else 'Down'
    file_info = read_status_file()
    return f'Site is {status}\n{file_info}'

bot.polling(none_stop=True, interval=0)

The bot works, but it only sends ‘Site is Up’ or ‘Site is Down’ without the extra info from the file. Any ideas why the file content isn’t showing up in the response? Thanks!

It seems the issue might be with the way you’re handling the file content in your check_status() function. Try modifying it to ensure the file content is properly incorporated:

def check_status():
    site_response = requests.get(site_url)
    status = 'Up' if site_response.status_code == 200 else 'Down'
    file_info = read_status_file()
    if file_info:
        return f'Site is {status}\nAdditional Info: {file_info}'
    else:
        return f'Site is {status}\nNo additional info available'

This way, you’re explicitly checking if file_info has content before including it in the response. Also, consider adding some logging or print statements to debug and ensure read_status_file() is actually returning data. If the problem persists, check file permissions and the exact path of status_info.txt relative to your script.

I’ve encountered a similar issue before, and it might be related to file permissions or the bot’s working directory. Here are a couple of things you can try:

  1. Make sure the ‘status_info.txt’ file is in the same directory as your script.

  2. Use an absolute path for the file instead of a relative one. Replace your read_status_file() function with:

import os

def read_status_file():
    file_path = os.path.join(os.path.dirname(__file__), 'status_info.txt')
    with open(file_path, 'r') as f:
        return f.readline().strip()

This ensures the file is always found, regardless of where the script is run from.

  1. Double-check that the file actually contains content and that you have read permissions.

  2. Add some error handling to your read_status_file() function to catch and log any issues:

def read_status_file():
    try:
        with open('status_info.txt', 'r') as f:
            return f.readline().strip()
    except Exception as e:
        print(f'Error reading file: {e}')
        return 'File content unavailable'

This way, you’ll know if there’s a problem reading the file. Hope this helps!

hEy there! have u checked if the file is actually being read? try adding a print statement in ur read_status_file() function to see if it’s working. Also, make sure the file path is correct and the file isn’t empty. if that doesn’t help, maybe try using readlines() instead of readline() to get all the content. Good luck!