W
W
WOModerMra2020-11-29 20:18:39
Python
WOModerMra, 2020-11-29 20:18:39

How to make a variable from the user's text in telegrams?

import telebot
import pyautogui
import time

bot = telebot.TeleBot('')
TOKEN = ''
tb = telebot.TeleBot(TOKEN)
keyboard1 = telebot.types.ReplyKeyboardMarkup()
keyboard1.row('Хром', 'Скрин', 'НеСпящий')
keyboard1.row('Искать', 'Написать')

@bot.message_handler(commands=['start'])
def start_message(message):
    bot.send_message(message.chat.id, 'Начнём', reply_markup=keyboard1)


@bot.message_handler(content_types=['text'])
def send_text(message):
    if message.text.lower() == 'хром':
        pyautogui.moveTo(464, 1064, 1)
        pyautogui.click()
        time.sleep(3)
        pyautogui.screenshot('my_screenshot.png')
        photo = open('my_screenshot.png', 'rb')
        tb.send_photo(message.chat.id, photo)
        
    elif message.text.lower() == 'скрин':
        pyautogui.screenshot('my_screenshot.png')
        photo = open('my_screenshot.png', 'rb')
        tb.send_photo(message.chat.id, photo)

    elif message.text.lower() == 'неспящий':
        pyautogui.moveTo(500, 500, 1)
        pyautogui.moveTo(1, 1, 1)
        
    elif message.text.lower() == 'искать':
        pyautogui.hotkey('ctrl', 't')
        time.sleep(3)
        pyautogui.screenshot('my_screenshot.png')
        photo = open('my_screenshot.png', 'rb')
        tb.send_photo(message.chat.id, photo)
        
    elif message.text.lower() == 'написать':
        message = bot.send_message(message.chat.id, 'Введите текст')
        bot.send_message(message.chat.id, message)

bot.polling()

There is this code
in this place
elif message.text.lower() == 'написать':
        message = bot.send_message(message.chat.id, 'Введите текст')
        bot.send_message(message.chat.id, message)

You need to make a variable in order to display it later in the same elif
How to make a variable in telebot?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Emil Timerbaev, 2020-11-29
@MrDlop

@bot.message_handler(content_types=['text'])
def send_text(message):
    if message.text.lower() == 'хром':
        pyautogui.moveTo(464, 1064, 1)
        pyautogui.click()
        time.sleep(3)
        pyautogui.screenshot('my_screenshot.png')
        photo = open('my_screenshot.png', 'rb')
        tb.send_photo(message.chat.id, photo)
        
    elif message.text.lower() == 'скрин':
        pyautogui.screenshot('my_screenshot.png')
        photo = open('my_screenshot.png', 'rb')
        tb.send_photo(message.chat.id, photo)

    elif message.text.lower() == 'неспящий':
        pyautogui.moveTo(500, 500, 1)
        pyautogui.moveTo(1, 1, 1)
        
    elif message.text.lower() == 'искать':
        pyautogui.hotkey('ctrl', 't')
        time.sleep(3)
        pyautogui.screenshot('my_screenshot.png')
        photo = open('my_screenshot.png', 'rb')
        tb.send_photo(message.chat.id, photo)
        
    elif message.text.lower() == 'написать':
        message = bot.send_message(message.chat.id, 'Введите текст')
        bot.register_next_step_handler(message, msg_0)
def msg_0(message):
    msg=message.text
    bot.send_message(message.chat.id, message)
    bot.register_next_step_handler(message, send_text)
bot.polling()

F
First Second, 2020-11-29
@vmolostvov

elif message.text.lower() == 'написать':
        msg = bot.send_message(message.chat.id, 'Введите текст')
        bot.register_next_step_handler(msg, process_step) 
        bot.clear_step_handler(msg)

And accordingly, the function that registers the next step
@bot.message_handler(content_types=['text'])
def process_step(message):
    bot.send_message(message.chat.id, message.text)

O
o5a, 2020-11-29
@o5a

Judging by the logic, you want to be able to get the text requested after "Enter text"?
Then you need to use register_next_step_handler . The following function is written in it, which already accepts text input.

bot.send_message(message.chat.id, 'Введите текст')
bot.register_next_step_handler(message, get_text)

def get_text(message):
    message.text будет как раз запрашиваемый текст

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question