summaryrefslogtreecommitdiff
path: root/include/py/homekit/telegram/_botcontext.py
blob: a143bfeb9a7565aa490b0cd14e6f8f32d802a4f9 (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from typing import Optional, List

from telegram import Update, User, CallbackQuery
from telegram.constants import ParseMode
from telegram.ext import CallbackContext

from ._botdb import BotDatabase
from ._botlang import lang
from ._botutil import IgnoreMarkup, exc2text


class Context:
    _update: Optional[Update]
    _callback_context: Optional[CallbackContext]
    _markup_getter: callable
    db: Optional[BotDatabase]
    _user_lang: Optional[str]

    def __init__(self,
                 update: Optional[Update],
                 callback_context: Optional[CallbackContext],
                 markup_getter: callable,
                 store: Optional[BotDatabase]):
        self._update = update
        self._callback_context = callback_context
        self._markup_getter = markup_getter
        self._store = store
        self._user_lang = None

    async def reply(self, text, markup=None):
        if markup is None:
            markup = self._markup_getter(self)
        kwargs = dict(parse_mode=ParseMode.HTML)
        if not isinstance(markup, IgnoreMarkup):
            kwargs['reply_markup'] = markup
        return await self._update.message.reply_text(text, **kwargs)

    async def reply_exc(self, e: Exception) -> None:
        await self.reply(exc2text(e), markup=IgnoreMarkup())

    async def answer(self, text: str = None):
        await self.callback_query.answer(text)

    async def edit(self, text, markup=None):
        kwargs = dict(parse_mode=ParseMode.HTML)
        if not isinstance(markup, IgnoreMarkup):
            kwargs['reply_markup'] = markup
        await self.callback_query.edit_message_text(text, **kwargs)

    @property
    def text(self) -> str:
        return self._update.message.text

    @property
    def callback_query(self) -> CallbackQuery:
        return self._update.callback_query

    @property
    def args(self) -> Optional[List[str]]:
        return self._callback_context.args

    @property
    def user_id(self) -> int:
        return self.user.id

    @property
    def user_data(self):
        return self._callback_context.user_data

    @property
    def user(self) -> User:
        return self._update.effective_user

    @property
    def user_lang(self) -> str:
        if self._user_lang is None:
            self._user_lang = self._store.get_user_lang(self.user_id)
        return self._user_lang

    def lang(self, key: str, *args) -> str:
        return lang.get(key, self.user_lang, *args)

    def is_callback_context(self) -> bool:
        return self._update.callback_query \
               and self._update.callback_query.data \
               and self._update.callback_query.data != ''