1 | import {
|
2 | InlineKeyboardButton,
|
3 | KeyboardButton,
|
4 | KeyboardButtonRequestChat,
|
5 | } from './core/types/typegram'
|
6 |
|
7 | type Hideable<B> = B & { hide: boolean }
|
8 |
|
9 | export function text(
|
10 | text: string,
|
11 | hide = false
|
12 | ): Hideable<KeyboardButton.CommonButton> {
|
13 | return { text, hide }
|
14 | }
|
15 |
|
16 | export function contactRequest(
|
17 | text: string,
|
18 | hide = false
|
19 | ): Hideable<KeyboardButton.RequestContactButton> {
|
20 | return { text, request_contact: true, hide }
|
21 | }
|
22 |
|
23 | export function locationRequest(
|
24 | text: string,
|
25 | hide = false
|
26 | ): Hideable<KeyboardButton.RequestLocationButton> {
|
27 | return { text, request_location: true, hide }
|
28 | }
|
29 |
|
30 | export function pollRequest(
|
31 | text: string,
|
32 | type?: 'quiz' | 'regular',
|
33 | hide = false
|
34 | ): Hideable<KeyboardButton.RequestPollButton> {
|
35 | return { text, request_poll: { type }, hide }
|
36 | }
|
37 |
|
38 | export function userRequest(
|
39 | text: string,
|
40 |
|
41 | request_id: number,
|
42 | user_is_premium?: boolean,
|
43 | hide = false
|
44 | ): Hideable<KeyboardButton.RequestUserButton> {
|
45 | return { text, request_user: { request_id, user_is_premium }, hide }
|
46 | }
|
47 |
|
48 | export function botRequest(
|
49 | text: string,
|
50 |
|
51 | request_id: number,
|
52 | hide = false
|
53 | ): Hideable<KeyboardButton.RequestUserButton> {
|
54 | return { text, request_user: { request_id, user_is_bot: true }, hide }
|
55 | }
|
56 |
|
57 | type KeyboardButtonRequestGroup = Omit<
|
58 | KeyboardButtonRequestChat,
|
59 | 'request_id' | 'chat_is_channel'
|
60 | >
|
61 |
|
62 | export function groupRequest(
|
63 | text: string,
|
64 |
|
65 | request_id: number,
|
66 | extra?: KeyboardButtonRequestGroup,
|
67 | hide = false
|
68 | ): Hideable<KeyboardButton.RequestChatButton> {
|
69 | return {
|
70 | text,
|
71 | request_chat: { request_id, chat_is_channel: false, ...extra },
|
72 | hide,
|
73 | }
|
74 | }
|
75 |
|
76 | type KeyboardButtonRequestChannel = Omit<
|
77 | KeyboardButtonRequestChat,
|
78 | 'request_id' | 'chat_is_channel' | 'chat_is_forum'
|
79 | >
|
80 |
|
81 | export function channelRequest(
|
82 | text: string,
|
83 |
|
84 | request_id: number,
|
85 | extra?: KeyboardButtonRequestChannel,
|
86 | hide = false
|
87 | ): Hideable<KeyboardButton.RequestChatButton> {
|
88 | return {
|
89 | text,
|
90 | request_chat: { request_id, chat_is_channel: true, ...extra },
|
91 | hide,
|
92 | }
|
93 | }
|
94 |
|
95 | export function url(
|
96 | text: string,
|
97 | url: string,
|
98 | hide = false
|
99 | ): Hideable<InlineKeyboardButton.UrlButton> {
|
100 | return { text, url, hide }
|
101 | }
|
102 |
|
103 | export function callback(
|
104 | text: string,
|
105 | data: string,
|
106 | hide = false
|
107 | ): Hideable<InlineKeyboardButton.CallbackButton> {
|
108 | return { text, callback_data: data, hide }
|
109 | }
|
110 |
|
111 | export function switchToChat(
|
112 | text: string,
|
113 | value: string,
|
114 | hide = false
|
115 | ): Hideable<InlineKeyboardButton.SwitchInlineButton> {
|
116 | return { text, switch_inline_query: value, hide }
|
117 | }
|
118 |
|
119 | export function switchToCurrentChat(
|
120 | text: string,
|
121 | value: string,
|
122 | hide = false
|
123 | ): Hideable<InlineKeyboardButton.SwitchInlineCurrentChatButton> {
|
124 | return { text, switch_inline_query_current_chat: value, hide }
|
125 | }
|
126 |
|
127 | export function game(
|
128 | text: string,
|
129 | hide = false
|
130 | ): Hideable<InlineKeyboardButton.GameButton> {
|
131 | return { text, callback_game: {}, hide }
|
132 | }
|
133 |
|
134 | export function pay(
|
135 | text: string,
|
136 | hide = false
|
137 | ): Hideable<InlineKeyboardButton.PayButton> {
|
138 | return { text, pay: true, hide }
|
139 | }
|
140 |
|
141 | export function login(
|
142 | text: string,
|
143 | url: string,
|
144 | opts: {
|
145 | forward_text?: string
|
146 | bot_username?: string
|
147 | request_write_access?: boolean
|
148 | } = {},
|
149 | hide = false
|
150 | ): Hideable<InlineKeyboardButton.LoginButton> {
|
151 | return {
|
152 | text,
|
153 | login_url: { ...opts, url },
|
154 | hide,
|
155 | }
|
156 | }
|
157 |
|
158 | export function webApp(
|
159 | text: string,
|
160 | url: string,
|
161 | hide = false
|
162 |
|
163 | ): Hideable<InlineKeyboardButton.WebAppButton> {
|
164 | return {
|
165 | text,
|
166 | web_app: { url },
|
167 | hide,
|
168 | }
|
169 | }
|