A
A
Askhatbek2021-06-05 08:41:34
Python
Askhatbek, 2021-06-05 08:41:34

How can I send a file through the tg bot?

Please help with the bot, it should send documents from the array while having an interval (it already exists there, so no help is needed with this). Here is the code itself:

import telebot
from telebot import types
import time

bot = telebot.TeleBot('1624298351:AAEaXHWkRHp8L67cx7iMq4bB-zT98fesWw4')

documentList = [open('D:\\My files\\Desktop\\telegram\\price 16 April. xls', 'rb'),
open('D:\\My files\\Desktop\\telegram\\Price April 17 Jangi.xls', 'rb'),
open('D:\\My files\\Desktop \\telegram\\Drag Promotion Price Apr 19.xls', 'rb'),
open('D:\\My files\\Desktop\\telegram\\Price Apr 21 yangi.xls', 'rb')]

@ bot.
keyboard = types.InlineKeyboardMarkup(row_width=1)
begin_button = types.InlineKeyboardButton(text="Start", callback_data="test")
keyboard.add(begin_button)
bot.reply_to(message,
f'I send prices every day, {message. from_user.first_name} Click start to start '
f'submitting prices',
reply_markup=keyboard)

@bot.callback_query_handler(content_type=['document'], func=lambda call: True)
def callback_worker(callback):
for i in range(len(documentList)):
bot.send_document(callback.message.chat.id, documentList[i])
time.sleep(5)

bot.polling(none_stop=True)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jerwright, 2021-06-05
@Askhatbek

I propose the following option. We will store only the directory in the array.

documentList = ['D:\\My files\\Desktop\\telegram\\прайс 16 апрель.xls',
'D:\\My files\\Desktop\\telegram\\прайс 17 апрель янги.xls',
'D:\\My files\\Desktop\\telegram\\Драг Промоущн прайс 19 апрель.xls',
'D:\\My files\\Desktop\\telegram\\прайс 21 апрель янги.xls']

And the send function looks like this:
@bot.callback_query_handler(func=lambda call: True)
def callback_worker(callback):
    for row in documentList:
        bot.send_document(callback.message.chat.id, open(row, 'rb'))
        time.sleep(5)

It is not necessary to pass the content_types parameter in the arguments to @bot.callback_query_handler , since it is used in cases where the bot will respond exclusively to this type of message (can be text, files, videos, etc.).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question