Answer the question
In order to leave comments, you need to log in
Error writing data from the bot to a file?
Hey! Telegram bot writes incoming messages to text documents. Moreover, it writes all messages to only one dock, the one in which it was written first. those. for example, we write /api_id to the bot, then /api_hash, and the data that should go to api_hash.txt goes to api_id.txt. There is an idea that after starting the mb function, it does not stop.
from telethon import TelegramClient, events, sync
import time
from random import randint
import telebot
bot = telebot.TeleBot('TOKEN')
@bot.message_handler(commands=["start"])
def start(m, res=False):
bot.send_message(m.chat.id, 'Привет! Я скоро буду самой угрожающей спам-машиной современности. \n /api_id\n /api_hash\n /spamtext')
@bot.message_handler(commands=["api_id"])
def api_id(m, res=False):
bot.send_message(m.chat.id, 'Введите Api ID, полученное на сайте my.telegram.org')
@bot.message_handler(func=lambda message: True, content_types=['text'])
def msg(message):
print(message.text)
f = open('C:/Users/асер/Desktop/python/bot/api_id.txt', 'a')
f.write(message.text + '\n')
f.close()
bot.send_message(m.chat.id, 'Прекрасно!')
@bot.message_handler(commands=["api_hash"])
def api_hash(m, res=False):
bot.send_message(m.chat.id, 'Введите Api Hash, полученное на сайте my.telegram.org')
@bot.message_handler(func=lambda message: True, content_types=['text'])
def msg(message):
print(message.text)
f1 = open('C:/Users/асер/Desktop/python/bot/api_hash.txt', 'a')
f1.write(message.text + '\n')
f1.close()
bot.send_message(m.chat.id, 'Чудесно!')
@bot.message_handler(commands=["spamtext"])
def spamtext(m, res=False):
bot.send_message(m.chat.id, 'Введите текст для спама')
@bot.message_handler(func=lambda message: True, content_types=['text'])
def msg(message):
print(message.text)
f2 = open('C:/Users/асер/Desktop/python/bot/spamtext.txt', 'a')
f2.write(message.text + '\n')
f2.close()
bot.send_message(m.chat.id, 'Великолепно!')
bot.polling(none_stop=True, interval=0)
Answer the question
In order to leave comments, you need to log in
Because that's not how it's done. Because handlers are written as nested functions, they won't only work inside those functions. The first handler written is of the same type (in this case content_types=['text']) and will be used for all inputs.
In this case, the use of register_next_step_handler is appropriate when specifying a function that handles the next step of the operation. A lot has been written about her in detail, incl. on that website.
For this bot it will be something like this:
instead of
@bot.message_handler(commands=["api_id"])
def api_id(m, res=False):
bot.send_message(m.chat.id, 'Введите Api ID, полученное на сайте my.telegram.org')
@bot.message_handler(func=lambda message: True, content_types=['text'])
def msg(message):
print(message.text)
f = open('C:/Users/асер/Desktop/python/bot/api_id.txt', 'a')
f.write(message.text + '\n')
f.close()
bot.send_message(m.chat.id, 'Прекрасно!')
@bot.message_handler(commands=["api_id"])
def api_id(m, res=False):
bot.send_message(m.chat.id, 'Введите Api ID, полученное на сайте my.telegram.org')
# указываем запуск следующего шага обработки - свою функцию
bot.register_next_step_handler(message, get_api_id)
def get_api_id(message):
print(message.text)
with open('C:/Users/асер/Desktop/python/bot/api_id.txt', 'a') as f:
f.write(message.text + '\n')
bot.send_message(m.chat.id, 'Прекрасно!')
with open('C:\\Vash\\Put\\K\\Papke\\nuzhnoe_nazvanie.txt', 'a+') as file:
file.write('Необходимый для записи текст')
file.close()
print('All done!')
For some reason, in all cases, only the file that is specified in the menu item that the user selected for the first time is written, that is, if you put a breakpoint, you can see that the file headler is always the same, and only the message that was in the relevant paragraph, no matter what command is given.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question