Coverage for telegram_webapp_auth/data.py: 100%

43 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-02 20:25 +0000

1"""This module contains data structures used in the Telegram Web Apps API.""" 

2 

3import dataclasses 

4import enum 

5import typing 

6 

7from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey 

8 

9TEST_PUBLIC_KEY_STR = "40055058a4ee38156a06562e52eece92a771bcd8346a8c4615cb7376eddf72ec" 

10PROD_PUBLIC_KEY_STR = "e7bf03a2fa4602af4580703d88dda5bb59f32ed8b02a56c187fe7d34caed242d" 

11 

12TEST_PUBLIC_KEY_BYTES = bytes.fromhex(TEST_PUBLIC_KEY_STR) 

13PROD_PUBLIC_KEY_BYTES = bytes.fromhex(PROD_PUBLIC_KEY_STR) 

14 

15TEST_PUBLIC_KEY = Ed25519PublicKey.from_public_bytes(TEST_PUBLIC_KEY_BYTES) 

16PROD_PUBLIC_KEY = Ed25519PublicKey.from_public_bytes(PROD_PUBLIC_KEY_BYTES) 

17 

18 

19@dataclasses.dataclass 

20class WebAppUser: 

21 """Represents a Telegram user. 

22 

23 Links: 

24 https://core.telegram.org/bots/webapps#webappuser 

25 """ 

26 

27 id: int 

28 first_name: str 

29 is_bot: typing.Optional[bool] = None 

30 last_name: typing.Optional[str] = None 

31 username: typing.Optional[str] = None 

32 language_code: typing.Optional[str] = None 

33 is_premium: typing.Optional[bool] = None 

34 added_to_attachment_menu: typing.Optional[bool] = None 

35 allows_write_to_pm: typing.Optional[bool] = None 

36 photo_url: typing.Optional[str] = None 

37 

38 

39class ChatType(str, enum.Enum): 

40 """Represents the type of Telegram chat.""" 

41 

42 SENDER = "sender" 

43 PRIVATE = "private" 

44 GROUP = "group" 

45 SUPERGROUP = "supergroup" 

46 CHANNEL = "channel" 

47 

48 

49@dataclasses.dataclass 

50class WebAppChat: 

51 """Represents a Telegram chat. 

52 

53 Links: 

54 https://core.telegram.org/bots/webapps#webappchat 

55 """ 

56 

57 id: int 

58 type: ChatType 

59 title: str 

60 username: typing.Optional[str] = None 

61 photo_url: typing.Optional[str] = None 

62 

63 

64@dataclasses.dataclass 

65class WebAppInitData: 

66 """Represents the data that the webapp receives from Telegram. 

67 

68 Links: 

69 https://core.telegram.org/bots/webapps#webappinitdata 

70 """ 

71 

72 auth_date: int 

73 hash: typing.Optional[str] = None 

74 signature: typing.Optional[str] = None 

75 query_id: typing.Optional[str] = None 

76 user: typing.Optional[WebAppUser] = None 

77 receiver: typing.Optional[WebAppUser] = None 

78 chat: typing.Optional[WebAppChat] = None 

79 chat_type: typing.Optional[ChatType] = None 

80 chat_instance: typing.Optional[str] = None 

81 start_param: typing.Optional[str] = None 

82 can_send_after: typing.Optional[int] = None 

83 extra: dict[str, str] = dataclasses.field(default_factory=dict)