summaryrefslogtreecommitdiff
path: root/include/py/homekit/telegram/_botutil.py
blob: 4fbbf284f7d451e81145578e87d6cc70cf901a80 (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
import logging
import traceback

from html import escape
from telegram import User

_logger = logging.getLogger(__name__)


def user_any_name(user: User) -> str:
    name = [user.first_name, user.last_name]
    name = list(filter(lambda s: s is not None, name))
    name = ' '.join(name).strip()

    if not name:
        name = user.username

    if not name:
        name = str(user.id)

    return name


def exc2text(e: Exception) -> str:
    tb = ''.join(traceback.format_tb(e.__traceback__))
    return f'{e.__class__.__name__}: ' + escape(str(e)) + "\n\n" + escape(tb)


class IgnoreMarkup:
    pass