Jul-06-2023, 01:52 AM
how to read a text file called file.log and send to a bot in telegram.
I have the latest version of python, python-telegram-bot
I have the latest version of python, python-telegram-bot
#!/usr/bin/env python
# pylint: disable=unused-argument, wrong-import-position
# This program is dedicated to the public domain under the CC0 license.
"""
Simple Bot to reply to Telegram messages.
First, a few handler functions are defined. Then, those functions are passed to
the Application and registered at their respective places.
Then, the bot is started and runs until we press Ctrl-C on the command line.
Usage:
Basic Echobot example, repeats messages.
Press Ctrl-C on the command line or send a signal to the process to stop the
bot.
"""
import logging
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import ForceReply, Update
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, CallbackContext
TOKEN = 'token_here'
def read_text_file(update: Update, context: CallbackContext):
# Get the path of the text file
file_path = 'C:/Users/daxx/Desktop/file.log'
# Check if the file exists
if not os.path.exists(file_path):
context.bot.send_message(chat_id=update.message.chat_id, text='File not found.')
return
# Read the content of the text file
with open(file_path, 'r') as file:
content = file.read()
# Send the content as a message
context.bot.send_message(chat_id=update.message.chat_id, text=content)
if __name__ == '__main__':
main()
