UNPKG

3.72 kBPlain TextView Raw
1import {
2 InlineKeyboardButton,
3 KeyboardButton,
4 KeyboardButtonRequestChat,
5} from './core/types/typegram'
6
7type Hideable<B> = B & { hide: boolean }
8
9export function text(
10 text: string,
11 hide = false
12): Hideable<KeyboardButton.CommonButton> {
13 return { text, hide }
14}
15
16export function contactRequest(
17 text: string,
18 hide = false
19): Hideable<KeyboardButton.RequestContactButton> {
20 return { text, request_contact: true, hide }
21}
22
23export function locationRequest(
24 text: string,
25 hide = false
26): Hideable<KeyboardButton.RequestLocationButton> {
27 return { text, request_location: true, hide }
28}
29
30export 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
38export function userRequest(
39 text: string,
40 /** Must fit in a signed 32 bit int */
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
48export function botRequest(
49 text: string,
50 /** Must fit in a signed 32 bit int */
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
57type KeyboardButtonRequestGroup = Omit<
58 KeyboardButtonRequestChat,
59 'request_id' | 'chat_is_channel'
60>
61
62export function groupRequest(
63 text: string,
64 /** Must fit in a signed 32 bit int */
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
76type KeyboardButtonRequestChannel = Omit<
77 KeyboardButtonRequestChat,
78 'request_id' | 'chat_is_channel' | 'chat_is_forum'
79>
80
81export function channelRequest(
82 text: string,
83 /** Must fit in a signed 32 bit int */
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
95export function url(
96 text: string,
97 url: string,
98 hide = false
99): Hideable<InlineKeyboardButton.UrlButton> {
100 return { text, url, hide }
101}
102
103export function callback(
104 text: string,
105 data: string,
106 hide = false
107): Hideable<InlineKeyboardButton.CallbackButton> {
108 return { text, callback_data: data, hide }
109}
110
111export 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
119export 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
127export function game(
128 text: string,
129 hide = false
130): Hideable<InlineKeyboardButton.GameButton> {
131 return { text, callback_game: {}, hide }
132}
133
134export function pay(
135 text: string,
136 hide = false
137): Hideable<InlineKeyboardButton.PayButton> {
138 return { text, pay: true, hide }
139}
140
141export 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
158export function webApp(
159 text: string,
160 url: string,
161 hide = false
162 // works as both InlineKeyboardButton and KeyboardButton
163): Hideable<InlineKeyboardButton.WebAppButton> {
164 return {
165 text,
166 web_app: { url },
167 hide,
168 }
169}