2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/telegram/DialogId.h"
|
|
|
|
#include "td/telegram/files/FileId.h"
|
|
|
|
#include "td/telegram/Location.h"
|
2018-09-28 22:57:34 +02:00
|
|
|
#include "td/telegram/MessageContent.h"
|
2018-01-30 18:06:54 +01:00
|
|
|
#include "td/telegram/MessageEntity.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/net/NetQuery.h"
|
|
|
|
#include "td/telegram/Photo.h"
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/UserId.h"
|
|
|
|
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
#include "td/actor/MultiPromise.h"
|
2022-07-02 22:32:18 +02:00
|
|
|
#include "td/actor/MultiTimeout.h"
|
2021-09-18 23:47:05 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/common.h"
|
2022-02-07 22:04:34 +01:00
|
|
|
#include "td/utils/FlatHashMap.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class Td;
|
|
|
|
|
|
|
|
class Game;
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class InlineQueriesManager final : public Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
InlineQueriesManager(Td *td, ActorShared<> parent);
|
|
|
|
|
|
|
|
void after_get_difference();
|
|
|
|
|
|
|
|
void answer_inline_query(int64 inline_query_id, bool is_personal,
|
2023-02-21 18:44:03 +01:00
|
|
|
td_api::object_ptr<td_api::inlineQueryResultsButton> &&button,
|
2022-03-24 09:31:33 +01:00
|
|
|
vector<td_api::object_ptr<td_api::InputInlineQueryResult>> &&input_results, int32 cache_time,
|
2023-02-21 18:44:03 +01:00
|
|
|
const string &next_offset, Promise<Unit> &&promise) const;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-03-30 15:27:01 +02:00
|
|
|
void get_simple_web_view_url(UserId bot_user_id, string &&url,
|
2022-08-25 22:15:53 +02:00
|
|
|
const td_api::object_ptr<td_api::themeParameters> &theme, string &&platform,
|
|
|
|
Promise<string> &&promise);
|
2022-03-30 15:27:01 +02:00
|
|
|
|
2022-03-28 14:18:15 +02:00
|
|
|
void send_web_view_data(UserId bot_user_id, string &&button_text, string &&data, Promise<Unit> &&promise) const;
|
|
|
|
|
2022-03-24 09:42:47 +01:00
|
|
|
void answer_web_view_query(const string &web_view_query_id,
|
|
|
|
td_api::object_ptr<td_api::InputInlineQueryResult> &&input_result,
|
2022-03-31 15:42:59 +02:00
|
|
|
Promise<td_api::object_ptr<td_api::sentWebAppMessage>> &&promise) const;
|
2022-03-24 09:42:47 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
uint64 send_inline_query(UserId bot_user_id, DialogId dialog_id, Location user_location, const string &query,
|
|
|
|
const string &offset, Promise<Unit> &&promise);
|
|
|
|
|
|
|
|
vector<UserId> get_recent_inline_bots(Promise<Unit> &&promise);
|
|
|
|
|
|
|
|
void remove_recent_inline_bot(UserId bot_user_id, Promise<Unit> &&promise);
|
|
|
|
|
2018-09-22 02:47:40 +02:00
|
|
|
const InlineMessageContent *get_inline_message_content(int64 query_id, const string &result_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
UserId get_inline_bot_user_id(int64 query_id) const;
|
|
|
|
|
2021-04-13 02:32:57 +02:00
|
|
|
void on_get_inline_query_results(DialogId dialog_id, UserId bot_user_id, uint64 query_hash,
|
2018-12-31 20:04:05 +01:00
|
|
|
tl_object_ptr<telegram_api::messages_botResults> &&results);
|
|
|
|
|
|
|
|
tl_object_ptr<td_api::inlineQueryResults> get_inline_query_results_object(uint64 query_hash);
|
|
|
|
|
2020-12-07 21:08:18 +01:00
|
|
|
void on_new_query(int64 query_id, UserId sender_user_id, Location user_location,
|
|
|
|
tl_object_ptr<telegram_api::InlineQueryPeerType> peer_type, const string &query,
|
2018-12-31 20:04:05 +01:00
|
|
|
const string &offset);
|
|
|
|
|
|
|
|
void on_chosen_result(UserId user_id, Location user_location, const string &query, const string &result_id,
|
2021-09-03 11:27:59 +02:00
|
|
|
tl_object_ptr<telegram_api::InputBotInlineMessageID> &&input_bot_inline_message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-03 11:27:59 +02:00
|
|
|
static int32 get_inline_message_dc_id(const tl_object_ptr<telegram_api::InputBotInlineMessageID> &inline_message_id);
|
|
|
|
|
|
|
|
static tl_object_ptr<telegram_api::InputBotInlineMessageID> get_input_bot_inline_message_id(
|
2018-12-31 20:04:05 +01:00
|
|
|
const string &inline_message_id);
|
|
|
|
|
|
|
|
static string get_inline_message_id(
|
2021-09-03 11:27:59 +02:00
|
|
|
tl_object_ptr<telegram_api::InputBotInlineMessageID> &&input_bot_inline_message_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
2023-10-01 20:17:30 +02:00
|
|
|
static constexpr size_t MAX_RECENT_INLINE_BOTS = 20; // some reasonable value
|
|
|
|
static constexpr int32 INLINE_QUERY_DELAY_MS = 400; // server side limit
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
static constexpr int32 BOT_INLINE_MEDIA_RESULT_FLAG_HAS_PHOTO = 1 << 0;
|
|
|
|
static constexpr int32 BOT_INLINE_MEDIA_RESULT_FLAG_HAS_DOCUMENT = 1 << 1;
|
|
|
|
static constexpr int32 BOT_INLINE_MEDIA_RESULT_FLAG_HAS_TITLE = 1 << 2;
|
|
|
|
static constexpr int32 BOT_INLINE_MEDIA_RESULT_FLAG_HAS_DESCRIPTION = 1 << 3;
|
|
|
|
|
2022-03-24 09:31:33 +01:00
|
|
|
Result<tl_object_ptr<telegram_api::InputBotInlineResult>> get_input_bot_inline_result(
|
|
|
|
td_api::object_ptr<td_api::InputInlineQueryResult> &&result, bool *is_gallery, bool *force_vertical) const;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Result<tl_object_ptr<telegram_api::InputBotInlineMessage>> get_inline_message(
|
|
|
|
tl_object_ptr<td_api::InputMessageContent> &&input_message_content,
|
|
|
|
tl_object_ptr<td_api::ReplyMarkup> &&reply_markup_ptr,
|
2018-09-27 20:14:32 +02:00
|
|
|
int32 allowed_media_content_id) const TD_WARN_UNUSED_RESULT;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
bool register_inline_message_content(int64 query_id, const string &result_id, FileId file_id,
|
|
|
|
tl_object_ptr<telegram_api::BotInlineMessage> &&inline_message,
|
2021-04-13 02:32:57 +02:00
|
|
|
int32 allowed_media_content_id, bool allow_invoice, Photo *photo = nullptr,
|
|
|
|
Game *game = nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-05-31 21:22:15 +02:00
|
|
|
tl_object_ptr<td_api::thumbnail> register_thumbnail(
|
2018-03-03 22:33:26 +01:00
|
|
|
tl_object_ptr<telegram_api::WebDocument> &&web_document_ptr) const;
|
|
|
|
|
|
|
|
static string get_web_document_url(const tl_object_ptr<telegram_api::WebDocument> &web_document_ptr);
|
|
|
|
static string get_web_document_content_type(const tl_object_ptr<telegram_api::WebDocument> &web_document_ptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
bool update_bot_usage(UserId bot_user_id);
|
|
|
|
|
|
|
|
void save_recently_used_bots();
|
|
|
|
bool load_recently_used_bots(Promise<Unit> &promise);
|
|
|
|
|
|
|
|
tl_object_ptr<td_api::inlineQueryResults> decrease_pending_request_count(uint64 query_hash);
|
|
|
|
|
|
|
|
static void on_drop_inline_query_result_timeout_callback(void *inline_queries_manager_ptr, int64 query_hash);
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
int32 recently_used_bots_loaded_ = 0; // 0 - not loaded, 1 - load request was sent, 2 - loaded
|
2018-12-12 00:48:56 +01:00
|
|
|
MultiPromiseActor resolve_recent_inline_bots_multipromise_{"ResolveRecentInlineBotsMultiPromiseActor"};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
vector<UserId> recently_used_bot_user_ids_;
|
|
|
|
|
|
|
|
struct PendingInlineQuery {
|
|
|
|
uint64 query_hash;
|
|
|
|
UserId bot_user_id;
|
2021-04-13 02:32:57 +02:00
|
|
|
DialogId dialog_id;
|
2020-12-07 23:26:40 +01:00
|
|
|
tl_object_ptr<telegram_api::InputPeer> input_peer;
|
2018-12-31 20:04:05 +01:00
|
|
|
Location user_location;
|
|
|
|
string query;
|
|
|
|
string offset;
|
|
|
|
Promise<Unit> promise;
|
|
|
|
};
|
|
|
|
|
2020-11-21 14:34:39 +01:00
|
|
|
double next_inline_query_time_ = 0.0;
|
2018-12-31 20:04:05 +01:00
|
|
|
unique_ptr<PendingInlineQuery> pending_inline_query_;
|
|
|
|
NetQueryRef sent_query_;
|
|
|
|
|
|
|
|
struct InlineQueryResult {
|
|
|
|
tl_object_ptr<td_api::inlineQueryResults> results;
|
|
|
|
double cache_expire_time;
|
|
|
|
int32 pending_request_count;
|
|
|
|
};
|
|
|
|
|
2018-08-01 19:31:20 +02:00
|
|
|
MultiTimeout drop_inline_query_result_timeout_{"DropInlineQueryResultTimeout"};
|
2022-02-07 20:41:07 +01:00
|
|
|
FlatHashMap<uint64, InlineQueryResult> inline_query_results_; // query_hash -> result
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-02-07 20:41:07 +01:00
|
|
|
FlatHashMap<int64, FlatHashMap<string, InlineMessageContent>>
|
2018-12-31 20:04:05 +01:00
|
|
|
inline_message_contents_; // query_id -> [result_id -> inline_message_content]
|
|
|
|
|
2022-02-07 20:41:07 +01:00
|
|
|
FlatHashMap<int64, UserId> query_id_to_bot_user_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Td *td_;
|
|
|
|
ActorShared<> parent_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|