-1

I have a Telegram bot which responds with different messages depending on the value funcnex.state has.

Function is operating with this class:

class FuncCreator:
    def __init__(self, start_state):
        self.state = start_state

    def __call__(self):
        print(self.state)
        self.state += 1

funcnex = FuncCreator(0)

The answer message is defined like this:

 elif ' Next' in text:
            if funcnex.state == 0:
                b2 = u'\U0001F532'+' Next'
                line1 = linecache.getline('stattxt/sc1-1.txt', 6)
                line2 = linecache.getline('stattxt/sc1-1.txt', 7)
                json_keyboard = json.dumps({keym: [[b2]], 'resize_keyboard': True, 'one_time_keyboard': False})
                reply(u'\U000025FC'+line1+'\n'+line2)              
                funcnex()
            elif funcnex.state == 1:
                b2 = u'\U000027A1'+' Next'
                line1 = linecache.getline('stattxt/sc1-1.txt', 12)
                line2 = linecache.getline('stattxt/sc1-1.txt', 13)
                json_keyboard = json.dumps({keym: [[b2]], 'resize_keyboard': True, 'one_time_keyboard': False})
                reply(u'\U0001F532'+line1+'\n'+line2)                
                funcnex()

The problem is that if several users interact with the bot, the funcnex.state for the new user could already be 1 or higher, if some user previously interacted with the bot.

The question is - how can I reset it for every new user?

Meaning that, for example,

If one user sends the word Next to the bot for the first time, they would receive answer 1. When he sends the word Next for second time, he would receive answer 2, and so on.

To count amount of times user send word Next, I am using the function that is in the example. If there would be only one user everything works fine, but for second user when counter should have value 0 for the start, now he gets the value of counter that previous user gained.

Jason
  • 2,278
  • 2
  • 17
  • 25
Alex Kedrov
  • 21
  • 1
  • 7
  • Why not create an instance per user? – jonrsharpe Nov 21 '16 at 19:38
  • Thank you for response! What instance do you mean? How it could look like? Sorry for the noob questions, this is my student project. – Alex Kedrov Nov 21 '16 at 19:40
  • Of `FuncCreator`, the thing that's doing the counting. Create a new one for each new user. – jonrsharpe Nov 21 '16 at 19:41
  • Could you share an idea how it may look like? – Alex Kedrov Nov 21 '16 at 19:43
  • It looks like exactly how you currently create one. – jonrsharpe Nov 21 '16 at 19:43
  • Sorry, I don't understand. I have unique varibale in `class WebhookHandler(webapp2.RequestHandler)` which is `chat_id = chat['id']`. How would I implement this in `FuncCreator` for every chat, so it would count separately for each user? – Alex Kedrov Nov 21 '16 at 19:48
  • Using variables in this case is a bad practice. What is the reason in this usage? – anatol Nov 23 '16 at 04:56
  • @anatol - I feel that it's not good, but I need somehow to track user answers. Meaning that, for example, If one user sends to bot word "Next" for the first time - he receives answer 1, when he sends the word "Next" for second time, he receives answer 2 and so on. To count amount of times user send word "Next" I use function that is in example. If there would be only one user everything works fine, but for second user counter should has 0 value for the start, but now he gets the value of counter that previous user gained. Please, share idea how to manage it proper way. – Alex Kedrov Nov 23 '16 at 06:06
  • Should you add this description to your question? – anatol Nov 23 '16 at 06:08
  • @anatol updated, thanks. – Alex Kedrov Nov 23 '16 at 06:09

1 Answers1

0

Here is a simple solution for this situation - take a look at InlineKeyboardMarkup. It's more elegantly for you instead variables etc. Just set callback_data for InlineKeyboardButton, catch it after user click, do something and change callback_data at the end.
For example bot sends message to user with inline button there text "Next" and callback data is 0. After user click you need catch CallbackQuery in your Update results. If it equal 0 - do something for this. Then you need change message with inline button - use Updating messages. Edit message and set for inline button new value of callback_data.

anatol
  • 1,680
  • 2
  • 24
  • 47