@WOModerMra

Как из текста пользователя в телеграмм сделать переменную?

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()

Есть этот код
В этом месте
elif message.text.lower() == 'написать':
        message = bot.send_message(message.chat.id, 'Введите текст')
        bot.send_message(message.chat.id, message)

Нужно сделать переменную, чтоб позже вывести её в этом же самом elif
Как сделать переменную в telebot?
  • Вопрос задан
  • 176 просмотров
Пригласить эксперта
Ответы на вопрос 3
@MrDlop
I love C++
@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()
Ответ написан
vmolostvov
@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)


И соответственно, функция регистрирующая следующий шаг

@bot.message_handler(content_types=['text'])
def process_step(message):
    bot.send_message(message.chat.id, message.text)
Ответ написан
Комментировать
@o5a
Судя по логике, хотите иметь возможность получить запрашиваемый после "Введите текст" текст?
Тогда нужно использовать register_next_step_handler. В нем прописывается следующая функция, которая принимает уже ввод текста.

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

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

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы