-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontext_manager.py
40 lines (32 loc) · 1.3 KB
/
context_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# from telegram.update import Update
from .action import Action
class ContextManager:
"""
The store house for the application data.
Data for a particular session (user:chat) is stored in self.contxt
All the subscribers can store their data for a particular session here.
This can be used to model the interactions with the database when the application scales.
"""
def __init__(self):
self.context = {}
self.actions = {}
self.start_action = Action('start', 'C')
def resolve(self, update):
user_id = update.message.message_from.id
chat_id = update.message.chat_id
key = '{}:{}'.format(user_id, chat_id)
if key not in self.context:
self.context[key] = {
'user_id': user_id,
'chat_id': chat_id,
'subscriptions': {},
'last_action': self.start_action.id
}
return self.context[key]
def add_actions(self, action):
if not isinstance(action, Action):
raise TypeError('`action` must be an instance of class:Action')
self.actions[action.id] = action
ContextManager = ContextManager() # ContextManager is a singleton
# contextManager = ContextManager # synonyms for ContextManager
# context_manager = ContextManager