K
K
khadjiru6662021-04-11 01:02:31
Python
khadjiru666, 2021-04-11 01:02:31

How to make a Telegram bot (File Transfer)?

Earlier I asked a question about such a bot (apparently deleted). In general, my task was to make a bot that would throw some kind of file (txt.), Or rather its data, during the N-number of intervals.
Here is the code

import telebot
from telebot import types

bot = telebot.TeleBot('ВАШ ТОКЕН')

def main():
    markup = types.ReplyKeyboardMarkup(True)
    key1 = types.KeyboardButton('Статус Взлома')
    markup.add(key1)
    return markup

@bot.message_handler(commands=['Статус Взлома'])
def start(message):
    bot.send_message(message.chat.id, 'Привет', reply_markup=main())

@bot.message_handler(content_types=['text'])
def cont(message):
    with open('example.txt') as misc:
        f = open('example.txt', 'rb')
        bot.send_document(message.chat.id, f)
bot.polling()



Свою задачу выполняет, но когда даешь команду(нажимаешь кнопку).
Как сделать чтобы он отправлял файл сам? Без команды

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alegzz, 2021-04-11
@alegzz

you can track changes in a file using the watchdog module:

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import time

class MyHandler(FileSystemEventHandler):
    def on_modified(self, event):
        print('Файл {} изменился'.format(event.src_path))

observer = Observer()
observer.schedule(MyHandler(), path="example.txt", recursive=False)
observer.start()
while True:
    time.sleep(1)

C
CleanyBoom, 2021-04-11
​​@CleanyBoom

Ahem. Just take it out for pulling and send a message separately

chat_id = 746698178 # сюда чат айди
f = open('example.txt', 'rb')
bot.send_document(chat_id, f)

A chat id can be obtained for example like this
@bot.message_handler(content_types=['text'])
def mess(message):
    bot.send_message(message.chat.id, message.chat.id) # Бот на текстовое сообщение вернет твой чат айди

PS Example of work first 2 codes then 1
6072a641ad9fe550063528.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question