10

I am building some telegram bot on python (using this framework pyTelegramBotAPI). And I ran into the problem with user input. I need save user input(it can be any text) after certain bot's message. For example:

Bot: - Please describe your problem.

User: - Our computer doesn't work.

Then I need to save this text "Our computer doesn't work" to some variable and go to the next step. Here's my code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import telebot
import constants
from telebot import types

bot = telebot.TeleBot(constants.token)

@bot.message_handler(commands=['start'])
def handle_start(message):
    keyboard = types.InlineKeyboardMarkup()
    callback_button = types.InlineKeyboardButton(text="Help me!", callback_data="start")
    keyboard.add(callback_button)
    bot.send_message(message.chat.id, "Welcome I am helper bot!", reply_markup=keyboard)



@bot.inline_handler(lambda query: len(query.query) > 0)
def query_text(query):
    kb = types.InlineKeyboardMarkup()
    kb.add(types.InlineKeyboardButton(text="Help me!", callback_data="start"))
    results = []
    single_msg = types.InlineQueryResultArticle(
        id="1", title="Press me",
        input_message_content=types.InputTextMessageContent(message_text="Welcome I am helper bot!"),
        reply_markup=kb
    )
    results.append(single_msg)
    bot.answer_inline_query(query.id, results)

@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
    if call.message:
        if call.data == "start":
            bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="Please describe your problem.")
            #here I need wait for user text response, save it and go to the next step

I have the idea with using message_id in statement, but still can't implement it. How I can solve this? Any ideas? Thank you.

user3151148
  • 375
  • 2
  • 3
  • 8

5 Answers5

6

This will help you https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/step_example.py

import telebot
import constants
from telebot import types

bot = telebot.TeleBot(constants.token)

@bot.message_handler(commands=['start'])
def start(message):
  sent = bot.send_message(message.chat.id, 'Please describe your problem.')
  bot.register_next_step_handler(sent, hello)

def hello(message):
    open('problem.txt', 'w').write(message.chat.id + ' | ' + message.text + '||')
    bot.send_message(message.chat.id, 'Thank you!')
    bot.send_message(ADMIN_ID, message.chat.id + ' | ' + message.text)

bot.polling() 
3

its not a python or even programming related question. its more like designing problem. but anyway. the solution is keeping session for users. for example user send you:

Our computer doesn't work.

at first you create a session for this user(the identity should be a user id) then send him/her a proper message. when user send the next message at first you look at the user status and see if he/she has a session or not. if he/she has session you continue with second step. i develop a bot like this and used dictionary in order to store users sessions. but it make all thing little complicated.

Mohammad
  • 2,724
  • 6
  • 29
  • 55
1

You can use Forcereply. Upon receiving a message with this object, Telegram clients will display a reply interface to the user (act as if the user has selected the bot‘s message and tapped ’Reply'). This can be extremely useful if you want to create user-friendly step-by-step interfaces without having to sacrifice privacy mode. https://core.telegram.org/bots/api#forcereply

user2448849
  • 37
  • 1
  • 1
  • 8
1

i know how fix that. It took 3 years for solution.

Just look at my code and make your own. Thx

@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call): #your call
    UserID = str(call.message.chat.id)

    if call.data == 'PhotoTEXT': 
       @bot.message_handler(content_types=['text'])
       def SetTEXTonPHOTO(message):  # THIS FUNCTION
           sent = bot.send_message(message.chat.id,'send me text')
           bot.register_next_step_handler(sent, TextONphoto)

           def TextONphoto(message): #Thsi function add text on photo
              im = Image.open('UserData/Files/photos/not_converted/'+UserID+'.jpg')
              idraw = ImageDraw.Draw(im)
              text = message.text
              font = ImageFont.truetype("arial.ttf", size=18)
              idraw.text((10, 10), text, font=font)  
              im.save('UserData/Files/photos/converted/'+UserID+'.jpg')
              im.show()

    SetTEXTonPHOTO(call.message) #just run your function
    with open('UserData/Files/photos/converted/'+UserID+'.jpg', 'rb') as file:
         bot.send_document(UserID,file)

I think i helped you friends <3

OLD Man
  • 11
  • 1
  • 1
    Welcome to stack overflow and that you for answering your first question. This is much appreciated! It would help the users though if you were a bit more specific as to how exactly your code help the user with his problem. – piterbarg Nov 08 '20 at 21:01
  • Плюсанул за ник и аватарку Дедушка – Антон Игнатьев Jul 01 '22 at 08:37
0

You should save the data in cache or database.

ManzoorWani
  • 1,016
  • 7
  • 14