A
A
Andriy4262021-09-06 19:09:35
Python
Andriy426, 2021-09-06 19:09:35

How to record messages that the user sends to the bot in a notepad ??

The user sends messages in a personal message to the bot. The bot sees the messages and writes it to a notepad or outputs it to the console.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vindicar, 2021-09-06
@Vindicar

You put a handler to receive a message (how it depends on the library used), in it you already do print or output to a file.
For the python-telegram-bot package, this would be something like

from telegram.ext import MessageHandler, Filters

def on_message(update, context):
    pass #тут делаешь что хочешь с update.message.text

message_handler = MessageHandler(Filters.text & (~Filters.command), on_message)
dispatcher.add_handler(message_handler)

The code is not complete , more details can be found in the documentation .
To be honest, the phrase "write in a notebook" makes you suspect that it's too early for you to deal with bots. First, learn the basics of programming.

R
Ruslan Mandzyuk, 2021-09-06
@Ryslan_13

from aiogram import Bot, Dispatcher, executor, types
from config import TOKEN

bot = Bot(TOKEN)
dp = Dispatcher(bot)

@dp.message_handler()
async def tex(message: types.Message):
  await bot.send_message(message.chat.id, message.text)
  with open('tee.txt', 'a+') as file_tee:
    file_tee.write(f'{message.from_user.first_name} - {message.text}\n')

if __name__ == '__main__':
  executor.start_polling(dp, skip_updates=True)

This line of code logs the user's messages (only the user)
with open('tee.txt', 'a+') as file_tee:
    file_tee.write(f'{message.from_user.first_name} - {message.text}\n')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question