FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tdesktop/Telegram/SourceFiles/api/api_bot.cpp at dev · dmitry/tdesktop · GitHub
dmitry
tdesktop
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
tdesktop
/
Telegram
/
SourceFiles
/
api
/
api_bot.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
607 lines (570 loc) · 16.3 KB
Breadcrumbs
tdesktop
/
Telegram
/
SourceFiles
/
api
/
api_bot.cpp
Copy path
File metadata and controls
607 lines (570 loc) · 16.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#
include
"
api/api_bot.h
"
#
include
"
apiwrap.h
"
#
include
"
api/api_cloud_password.h
"
#
include
"
api/api_send_progress.h
"
#
include
"
api/api_suggest_post.h
"
#
include
"
boxes/peers/choose_peer_box.h
"
#
include
"
boxes/peers/create_managed_bot_box.h
"
#
include
"
boxes/passcode_box.h
"
#
include
"
boxes/share_box.h
"
#
include
"
boxes/url_auth_box.h
"
#
include
"
lang/lang_keys.h
"
#
include
"
chat_helpers/bot_command.h
"
#
include
"
core/core_cloud_password.h
"
#
include
"
core/click_handler_types.h
"
#
include
"
data/data_changes.h
"
#
include
"
data/data_peer.h
"
#
include
"
data/data_poll.h
"
#
include
"
data/data_user.h
"
#
include
"
data/data_session.h
"
#
include
"
history/history.h
"
#
include
"
history/history_item.h
"
#
include
"
history/history_item_components.h
"
#
include
"
inline_bots/bot_attach_web_view.h
"
#
include
"
payments/payments_checkout_process.h
"
#
include
"
payments/payments_non_panel_process.h
"
#
include
"
main/main_session.h
"
#
include
"
mainwidget.h
"
#
include
"
mainwindow.h
"
#
include
"
window/window_session_controller.h
"
#
include
"
window/window_peer_menu.h
"
#
include
"
ui/boxes/confirm_box.h
"
#
include
"
ui/toast/toast.h
"
#
include
"
ui/layers/generic_box.h
"
#
include
"
ui/text/text_utilities.h
"
#
include
"
styles/style_chat.h
"
#
include
<
QtCore/QDataStream
>
#
include
<
QtGui/QGuiApplication
>
#
include
<
QtGui/QClipboard
>
namespace
Api
{
namespace
{
void
SendBotCallbackData
(
not_null<Window::SessionController*> controller,
not_null<HistoryItem*> item,
int
row,
int
column,
std::optional<Core::CloudPasswordResult> password,
Fn<
void
()> done = nullptr,
Fn<void(
const
QString &)> handleError = nullptr) {
if
(!item->
isRegular
()) {
return
;
}
const
auto
history = item->
history
();
const
auto
session = &history->
session
();
const
auto
owner = &history->
owner
();
const
auto
api = &session->
api
();
const
auto
bot = item->
getMessageBot
();
const
auto
fullId = item->
fullId
();
const
auto
getButton = [=] {
return
HistoryMessageMarkupButton::Get
(owner, fullId, row, column);
};
const
auto
button =
getButton
();
if
(!button || button->
requestId
) {
return
;
}
using
ButtonType = HistoryMessageMarkupButton::Type;
const
auto
isGame = (button->
type
== ButtonType::Game);
auto
flags =
MTPmessages_GetBotCallbackAnswer::Flags
(
0
);
QByteArray sendData;
if
(isGame) {
flags |= MTPmessages_GetBotCallbackAnswer::Flag::f_game;
}
else
if
(button->
type
== ButtonType::Callback
|| button->
type
== ButtonType::CallbackWithPassword) {
flags |= MTPmessages_GetBotCallbackAnswer::Flag::f_data;
sendData = button->
data
;
}
const
auto
withPassword = password.
has_value
();
if
(withPassword) {
flags |= MTPmessages_GetBotCallbackAnswer::Flag::f_password;
}
const
auto
weak =
base::make_weak
(controller);
const
auto
show = controller->
uiShow
();
button->
requestId
= api->
request
(
MTPmessages_GetBotCallbackAnswer
(
MTP_flags
(flags),
history->
peer
->
input
(),
MTP_int
(item->
id
),
MTP_bytes
(sendData),
password ? password->
result
:
MTP_inputCheckPasswordEmpty
()
)).
done
([=](
const
MTPmessages_BotCallbackAnswer &result) {
const
auto
guard =
gsl::finally
([&] {
if
(done) {
done
();
}
});
const
auto
item = owner->
message
(fullId);
if
(!item) {
return
;
}
if
(
const
auto
button =
getButton
()) {
button->
requestId
=
0
;
owner->
requestItemRepaint
(item);
}
const
auto
&data = result.
data
();
const
auto
message = data.
vmessage
()
?
qs
(*data.
vmessage
())
:
QString
();
const
auto
link = data.
vurl
() ?
qs
(*data.
vurl
()) :
QString
();
const
auto
showAlert = data.
is_alert
();
if
(!message.
isEmpty
()) {
if
(!show->
valid
()) {
return
;
}
else
if
(showAlert) {
show->
showBox
(
Ui::MakeInformBox
(message));
}
else
{
if
(withPassword) {
show->
hideLayer
();
}
show->
showToast
(message);
}
}
else
if
(!link.
isEmpty
()) {
if
(!isGame) {
UrlClickHandler::Open
(link);
return
;
}
BotGameUrlClickHandler
(bot, link).
onClick
({
Qt::LeftButton,
QVariant::fromValue
(ClickHandlerContext{
.
itemId
= item->
fullId
(),
.
sessionWindow
= weak,
}),
});
session->
sendProgressManager
().
update
(
history,
Api::SendProgressType::PlayGame);
}
else
if
(withPassword) {
show->
hideLayer
();
}
}).
fail
([=](
const
MTP
::Error &error) {
const
auto
guard =
gsl::finally
([&] {
if
(handleError) {
handleError
(error.
type
());
}
});
const
auto
item = owner->
message
(fullId);
if
(!item) {
return
;
}
//
Show error?
if
(
const
auto
button =
getButton
()) {
button->
requestId
=
0
;
owner->
requestItemRepaint
(item);
}
}).
send
();
session->
changes
().
messageUpdated
(
item,
Data::MessageUpdate::Flag::BotCallbackSent
);
}
void
HideSingleUseKeyboard
(
not_null<Window::SessionController*> controller,
not_null<HistoryItem*> item) {
controller->
content
()->
hideSingleUseKeyboard
(item->
fullId
());
}
}
//
namespace
void
SendBotCallbackData
(
not_null<Window::SessionController*> controller,
not_null<HistoryItem*> item,
int
row,
int
column) {
SendBotCallbackData
(controller, item, row, column, std::
nullopt
);
}
void
SendBotCallbackDataWithPassword
(
not_null<Window::SessionController*> controller,
not_null<HistoryItem*> item,
int
row,
int
column) {
if
(!item->
isRegular
()) {
return
;
}
const
auto
history = item->
history
();
const
auto
session = &history->
session
();
const
auto
owner = &history->
owner
();
const
auto
api = &session->
api
();
const
auto
fullId = item->
fullId
();
const
auto
getButton = [=] {
return
HistoryMessageMarkupButton::Get
(
owner,
fullId,
row,
column);
};
const
auto
button =
getButton
();
if
(!button || button->
requestId
) {
return
;
}
api->
cloudPassword
().
reload
();
const
auto
weak =
base::make_weak
(controller);
const
auto
show = controller->
uiShow
();
SendBotCallbackData
(controller, item, row, column, {}, {}, [=](
const
QString &error) {
auto
box =
PrePasswordErrorBox
(
error,
session,
tr::lng_bots_password_confirm_check_about
(
tr::now,
tr::marked));
if
(box) {
show->
showBox
(
std::move
(box), Ui::LayerOption::CloseOther);
}
else
{
auto
lifetime = std::make_shared<rpl::lifetime>();
button->
requestId
= -
1
;
api->
cloudPassword
().
state
(
) |
rpl::take
(
1
) |
rpl::on_next
([=](
const
Core::CloudPasswordState &state)
mutable
{
if
(lifetime) {
base::take
(lifetime)->
destroy
();
}
if
(
const
auto
button =
getButton
()) {
if
(button->
requestId
== -
1
) {
button->
requestId
=
0
;
}
}
else
{
return
;
}
auto
fields =
PasscodeBox::CloudFields::From
(state);
fields.
customTitle
=
tr::lng_bots_password_confirm_title
();
fields.
customDescription
=
tr::lng_bots_password_confirm_description
(tr::now);
fields.
customSubmitButton
=
tr::lng_passcode_submit
();
fields.
customCheckCallback
= [=](
const
Core::CloudPasswordResult &result,
base::weak_qptr<PasscodeBox> box) {
if
(
const
auto
button =
getButton
()) {
if
(button->
requestId
) {
return
;
}
}
else
{
return
;
}
if
(
const
auto
item = owner->
message
(fullId)) {
const
auto
strongController = weak.
get
();
if
(!strongController) {
return
;
}
SendBotCallbackData
(strongController, item, row, column, result, [=] {
if
(box) {
box->
closeBox
();
}
}, [=](
const
QString &error) {
if
(box) {
box->
handleCustomCheckError
(error);
}
});
}
};
auto
object = Box<PasscodeBox>(session, fields);
show->
showBox
(
std::move
(object), Ui::LayerOption::CloseOther);
}, *lifetime);
}
});
}
bool
SwitchInlineBotButtonReceived
(
not_null<Window::SessionController*> controller,
const
QByteArray &queryWithPeerTypes,
UserData *samePeerBot,
MsgId samePeerReplyTo) {
return
controller->
content
()->
notify_switchInlineBotButtonReceived
(
QString::fromUtf8
(queryWithPeerTypes),
samePeerBot,
samePeerReplyTo);
}
void
ActivateBotCommand
(ClickHandlerContext context,
int
row,
int
column) {
const
auto
strong = context.
sessionWindow
.
get
();
if
(!strong) {
return
;
}
const
auto
controller = not_null{ strong };
const
auto
item = controller->
session
().
data
().
message
(context.
itemId
);
if
(!item) {
return
;
}
const
auto
button =
HistoryMessageMarkupButton::Get
(
&item->
history
()->
owner
(),
item->
fullId
(),
row,
column);
if
(!button) {
return
;
}
using
ButtonType = HistoryMessageMarkupButton::Type;
switch
(button->
type
) {
case
ButtonType::Default: {
//
Copy string before passing it to the sending method
//
because the original button can be destroyed inside.
const
auto
replyTo = item->
isRegular
()
? item->
fullId
()
:
FullMsgId
();
controller->
content
()->
sendBotCommand
({
.
peer
= item->
history
()->
peer
,
.
command
=
QString
(button->
text
),
.
context
= item->
fullId
(),
.
replyTo
= { replyTo },
});
}
break
;
case
ButtonType::Callback:
case
ButtonType::Game: {
SendBotCallbackData
(controller, item, row, column);
}
break
;
case
ButtonType::CallbackWithPassword: {
SendBotCallbackDataWithPassword
(controller, item, row, column);
}
break
;
case
ButtonType::Buy: {
Payments::CheckoutProcess::Start
(
item,
Payments::Mode::Payment,
crl::guard
(controller, [=](
auto
) {
controller->
widget
()->
activate
();
}),
Payments::ProcessNonPanelPaymentFormFactory
(controller, item));
}
break
;
case
ButtonType::Url: {
auto
url =
QString::fromUtf8
(button->
data
);
auto
skipConfirmation =
false
;
if
(
const
auto
bot = item->
getMessageBot
()) {
if
(bot->
isVerified
()) {
skipConfirmation =
true
;
}
}
const
auto
variant =
QVariant::fromValue
(context);
if
(skipConfirmation) {
UrlClickHandler::Open
(url, variant);
}
else
{
HiddenUrlClickHandler::Open
(url, variant);
}
}
break
;
case
ButtonType::RequestLocation: {
HideSingleUseKeyboard
(controller, item);
controller->
show
(
Ui::MakeInformBox
(
tr::lng_bot_share_location_unavailable
()));
}
break
;
case
ButtonType::RequestPhone: {
HideSingleUseKeyboard
(controller, item);
const
auto
itemId = item->
fullId
();
const
auto
topicRootId = item->
topicRootId
();
const
auto
history = item->
history
();
controller->
show
(
Ui::MakeConfirmBox
({
.
text
=
tr::lng_bot_share_phone
(),
.
confirmed
= [=] {
controller->
showPeerHistory
(
history,
Window::SectionShow::Way::Forward,
ShowAtTheEndMsgId);
auto
action =
Api::SendAction
(history);
action.
clearDraft
=
false
;
action.
replyTo
= {
.
messageId
= itemId,
.
topicRootId
= topicRootId,
};
history->
session
().
api
().
shareContact
(
history->
session
().
user
(),
action);
},
.
confirmText
=
tr::lng_bot_share_phone_confirm
(),
}));
}
break
;
case
ButtonType::RequestPoll: {
HideSingleUseKeyboard
(controller, item);
auto
chosen =
kDefaultPollCreateFlags
;
auto
disabled =
PollData::Flags
();
if
(!button->
data
.
isEmpty
()) {
disabled |= PollData::Flag::Quiz;
if
(button->
data
[
0
]) {
chosen |= PollData::Flag::Quiz;
}
}
const
auto
replyTo =
FullReplyTo
();
const
auto
suggest =
SuggestOptions
();
Window::PeerMenuCreatePoll
(
controller,
item->
history
()->
peer
,
replyTo,
suggest,
chosen,
disabled);
}
break
;
case
ButtonType::RequestPeer: {
HideSingleUseKeyboard
(controller, item);
auto
query =
RequestPeerQuery
();
Assert
(button->
data
.
size
() ==
sizeof
(query));
memcpy
(&query, button->
data
.
data
(),
sizeof
(query));
const
auto
peer = item->
history
()->
peer
;
const
auto
itemId = item->
id
;
const
auto
id =
int32
(button->
buttonId
);
const
auto
chosen = [=](std::vector<not_null<PeerData*>> result) {
using
Flag = MTPmessages_SendBotRequestedPeer::Flag;
peer->
session
().
api
().
request
(
MTPmessages_SendBotRequestedPeer
(
MTP_flags
(Flag::f_msg_id),
peer->
input
(),
MTP_int
(itemId),
MTPstring
(),
//
request_id
MTP_int
(id),
MTP_vector_from_range
(
result |
ranges::views::transform
([](
not_null<PeerData*> peer) {
return
MTPInputPeer
(peer->
input
());
}))
)).
done
([=](
const
MTPUpdates &result) {
peer->
session
().
api
().
applyUpdates
(result);
}).
send
();
};
if
(
const
auto
bot = item->
getMessageBot
()) {
ShowChoosePeerBox
(controller, bot, query, chosen);
}
else
{
LOG
((
"
API Error: Bot not found for RequestPeer button.
"
));
}
}
break
;
case
ButtonType::SwitchInlineSame:
case
ButtonType::SwitchInline: {
if
(
const
auto
bot = item->
getMessageBot
()) {
const
auto
fastSwitchDone = [&] {
const
auto
samePeer = (button->
type
== ButtonType::SwitchInlineSame);
if
(samePeer) {
SwitchInlineBotButtonReceived
(
controller,
button->
data
,
bot,
item->
id
);
return
true
;
}
else
if
(bot->
isBot
() && bot->
botInfo
->
inlineReturnTo
.
key
) {
const
auto
switched =
SwitchInlineBotButtonReceived
(
controller,
button->
data
);
if
(switched) {
return
true
;
}
}
return
false
;
}();
if
(!fastSwitchDone) {
const
auto
query =
QString::fromUtf8
(button->
data
);
const
auto
chosen = [=](not_null<Data::Thread*> thread) {
return
controller->
switchInlineQuery
(
thread,
bot,
query);
};
Window::ShowChooseRecipientBox
(
controller,
chosen,
tr::lng_inline_switch_choose
(),
nullptr
,
button->
peerTypes
);
}
}
}
break
;
case
ButtonType::Auth:
UrlAuthBox::ActivateButton
(controller->
uiShow
(), item, row, column);
break
;
case
ButtonType::UserProfile: {
const
auto
session = &item->
history
()->
session
();
const
auto
userId =
UserId
(button->
data
.
toULongLong
());
if
(
const
auto
user = session->
data
().
userLoaded
(userId)) {
controller->
showPeerInfo
(user);
}
}
break
;
case
ButtonType::WebView: {
if
(
const
auto
bot = item->
getMessageBot
()) {
bot->
session
().
attachWebView
().
open
({
.
bot
= bot,
.
context
= { .
controller
= controller },
.
button
= { .
text
= button->
text
, .
url
= button->
data
},
.
source
= InlineBots::WebViewSourceButton{ .
simple
=
false
},
});
}
}
break
;
case
ButtonType::SimpleWebView: {
if
(
const
auto
bot = item->
getMessageBot
()) {
bot->
session
().
attachWebView
().
open
({
.
bot
= bot,
.
context
= { .
controller
= controller },
.
button
= { .
text
= button->
text
, .
url
= button->
data
},
.
source
= InlineBots::WebViewSourceButton{ .
simple
=
true
},
});
}
}
break
;
case
ButtonType::CopyText: {
const
auto
text =
QString::fromUtf8
(button->
data
);
if
(!text.
isEmpty
()) {
QGuiApplication::clipboard
()->
setText
(text);
controller->
showToast
(
tr::lng_text_copied
(tr::now));
}
}
break
;
case
ButtonType::SuggestAccept: {
Api::AcceptClickHandler
(item)->
onClick
(ClickContext{
Qt::LeftButton,
QVariant::fromValue
(context),
});
}
break
;
case
ButtonType::SuggestDecline: {
Api::DeclineClickHandler
(item)->
onClick
(ClickContext{
Qt::LeftButton,
QVariant::fromValue
(context),
});
}
break
;
case
ButtonType::SuggestChange: {
Api::SuggestChangesClickHandler
(item)->
onClick
(ClickContext{
Qt::LeftButton,
QVariant::fromValue
(context),
});
}
break
;
case
ButtonType::CreateBot: {
HideSingleUseKeyboard
(controller, item);
auto
suggestedName =
QString
();
auto
suggestedUsername =
QString
();
{
auto
stream =
QDataStream
(button->
data
);
stream >> suggestedName >> suggestedUsername;
}
const
auto
peer = item->
history
()->
peer
;
const
auto
itemId = item->
id
;
const
auto
id =
int32
(button->
buttonId
);
const
auto
bot = item->
getMessageBot
();
if
(!bot) {
break
;
}
ShowCreateManagedBotBox
({
.
show
= controller->
uiShow
(),
.
manager
= bot,
.
suggestedName
= suggestedName,
.
suggestedUsername
= suggestedUsername,
.
done
= [=](not_null<UserData*> createdBot) {
using
Flag = MTPmessages_SendBotRequestedPeer::Flag;
peer->
session
().
api
().
request
(
MTPmessages_SendBotRequestedPeer
(
MTP_flags
(Flag::f_msg_id),
peer->
input
(),
MTP_int
(itemId),
MTPstring
(),
MTP_int
(id),
MTP_vector<MTPInputPeer>(
1
,
createdBot->
input
()))
).
done
([=](
const
MTPUpdates &result) {
peer->
session
().
api
().
applyUpdates
(result);
}).
send
();
controller->
showPeerHistory
(createdBot);
controller->
showToast
({
.
title
=
tr::lng_managed_bot_created_title
(
tr::now,
lt_name,
createdBot->
name
()),
.
text
= {
tr::lng_managed_bot_created_text
(
tr::now,
lt_parent_name,
bot->
name
()) },
.
icon
= &st::toastCheckIcon,
});
},
});
}
break
;
}
}
}
//
namespace Api
Back
|
FazBrowse Home
|
New Git URL