1
votes

I followed a tutorial in youtube on how to create a Telegram Bot and for now it can only send messages but I want to send even files like documents or audio, video, photos etc... For now im just trying to send a file but I'm pretty confused and I don't know how to do it.

The source code of the bot is divided in 2 main files. One responser.py:

def responses(input_text):
    user_message = str(input_text).lower()

    if user_message in ("test", "testing"):
        return "123 working..."
    

    return "The command doesn't exists. Type /help to see the command options."

and main.py:

import constants as key
from telegram.ext import *
import responser as r

print("Hello. Cleint has just started.")

def start_command(update):
    update.message.reply_text("The Bot Has Started send you command sir.")



def help_command(update):
    update.message.reply_text("""
    Welcome to the Cleint Bot. For this purchase the following commands are available:
    send - send command is to send the log file from the other side of computer""")

def send_document(update, context):
    doc_file = open("image1.png", "rb")
    chat_id = update.effctive_chat.id
    return context.bot.send_document(chat_id, doc_file)

def handle_message(update, context):
    text = str(update.message.text).lower()
    response = r.responses(text)
    update.message.reply_text(response)

def error(update, context):
    print(f"Update {update} cause error: {context.error}")

def main():

    updater = Updater(key.API_KEY, use_context=True)
    dp = updater.dispatcher
    dp.add_handler(CommandHandler("start", start_command))
    dp.add_handler(CommandHandler("help", help_command))

    dp.add_handler(MessageHandler(Filters.text, handle_message))
    dp.add_error_handler(error)
    updater.start_polling()
    updater.idle()

main()

could someone help me out?

1

1 Answers

0
votes

Try the following as your send_document function:

def send_document(update, context):
    chat_id = update.message.chat_id
    document = open('image1.png', 'rb')
    context.bot.send_document(chat_id, document)

And add the command 'send' to the bot in the main function like this:

dp.add_handler(CommandHandler("send", send_document))

This will make it so if you type /send in Telegram, the bot will send you the document.