Home>

pyTelegramBotAPI cannot use other libraries as I have already done many functions for this bot. I need to add a function to the bot with which it will be possible to save the text from the user and + then change this text, it is best to save all this in .txt. Please help if you can)

Please correct the question to reflect the specific problem with enough detail to be able to provide an adequate answer.

Дух сообщества2021-11-14 20:21:11
  • Answer # 1

    In what sense should the text be saved? To get some text from the user use @ bot.message_handler or bot.register_next_step_handler, to change the message use bot.edit_message_text

    If you just want to receive his message and record, then use @ bot.message_handler:

    @ bot.message_handler (content_types= ["text"])
    def write (message):
        text= message.text
        f= open ("writes.txt", "a")
        f.write (text)
        f.close ()
    

    I want that when I enter, for example, the command /write Today is sunny, the bot will save it in writes.txt

    Билан2021-11-14 20:21:11

    Completed the answer.

    Blazzerrr2021-11-14 00:51:43

    but how to make it so that later you can change this text?

    Билан2021-11-14 05:56:03
  • Answer # 2

    It's very simple. When the user writes the command /write There was a storm yesterday, you need to separate the command and its arguments.

    1st way:

    data= message.text
    data= data.replace ("/write", "")
    

    Voila, you have all the rest of the text.

    2nd way:

    data= message.text
    data= data.split ("")
    data= data [1:]
    

    Voila and you already have all the data, but in the form of a list. This is generally done by built-in library functions. I did not write on your library, but I used it in a more powerful aiogram. There is a built-in parameter args. You do not, and all arguments entered with the command are accepted. You can google if your library has it.

    thank you helped)

    Билан2021-11-14 05:44:36