Add td_api::getCurrentWeather.

This commit is contained in:
levlam 2024-07-17 17:54:46 +03:00
parent d49f86e5c2
commit d79a350492
6 changed files with 91 additions and 3 deletions

View File

@ -4097,6 +4097,12 @@ emojiCategoryTypeEmojiStatus = EmojiCategoryType;
emojiCategoryTypeChatPhoto = EmojiCategoryType;
//@description Describes the current weather
//@temperature Temperature, in degree Celsius
//@emoji Emoji representing the weather
currentWeather temperature:double emoji:string = CurrentWeather;
//@description Describes position of a clickable rectangle area on a story media
//@x_percentage The abscissa of the rectangle's center, as a percentage of the media width
//@y_percentage The ordinate of the rectangle's center, as a percentage of the media height
@ -9619,6 +9625,10 @@ setPinnedChats chat_list:ChatList chat_ids:vector<int53> = Ok;
readChatList chat_list:ChatList = Ok;
//@description Returns the current weather in the given location @location The location
getCurrentWeather location:location = CurrentWeather;
//@description Returns a story
//@story_sender_chat_id Identifier of the chat that posted the story
//@story_id Story identifier

View File

@ -27,6 +27,7 @@
#include "td/telegram/MessageContentType.h"
#include "td/telegram/MessageEntity.h"
#include "td/telegram/misc.h"
#include "td/telegram/OptionManager.h"
#include "td/telegram/Photo.h"
#include "td/telegram/PhotoFormat.h"
#include "td/telegram/PhotoSize.h"
@ -47,6 +48,7 @@
#include "td/utils/algorithm.h"
#include "td/utils/base64.h"
#include "td/utils/buffer.h"
#include "td/utils/emoji.h"
#include "td/utils/HashTableUtils.h"
#include "td/utils/HttpUrl.h"
#include "td/utils/logging.h"
@ -985,12 +987,65 @@ Result<tl_object_ptr<telegram_api::InputBotInlineResult>> InlineQueriesManager::
flags, id, type, title, description, url, std::move(thumbnail), std::move(content), std::move(inline_message));
}
void InlineQueriesManager::get_weather(Location location,
Promise<td_api::object_ptr<td_api::currentWeather>> &&promise) {
if (location.empty()) {
return promise.set_error(Status::Error(400, "Location must be non-empty"));
}
auto bot_username = td_->option_manager_->get_option_string("weather_bot_username");
if (bot_username.empty()) {
LOG(ERROR) << "Have no weather bot";
return promise.set_error(Status::Error(500, "Not supported"));
}
td_->dialog_manager_->resolve_dialog(
bot_username, ChannelId(),
PromiseCreator::lambda([actor_id = actor_id(this), location = std::move(location),
promise = std::move(promise)](Result<DialogId> r_bot_dialog_id) mutable {
if (r_bot_dialog_id.is_error()) {
return promise.set_error(r_bot_dialog_id.move_as_error());
}
send_closure(actor_id, &InlineQueriesManager::do_get_weather, r_bot_dialog_id.ok(), std::move(location),
std::move(promise));
}));
}
void InlineQueriesManager::do_get_weather(DialogId dialog_id, Location location,
Promise<td_api::object_ptr<td_api::currentWeather>> &&promise) {
TRY_STATUS_PROMISE(promise, G()->close_status());
if (dialog_id.get_type() != DialogType::User) {
LOG(ERROR) << "Weather bot isn't a user";
return promise.set_error(Status::Error(500, "Not supported"));
}
send_inline_query(
dialog_id.get_user_id(), DialogId(), std::move(location), string(), string(),
PromiseCreator::lambda([actor_id = actor_id(this), promise = std::move(promise)](
Result<td_api::object_ptr<td_api::inlineQueryResults>> r_results) mutable {
if (r_results.is_error()) {
return promise.set_error(Status::Error(500, "Not supported"));
}
send_closure(actor_id, &InlineQueriesManager::on_get_weather, r_results.move_as_ok(), std::move(promise));
}));
}
void InlineQueriesManager::on_get_weather(td_api::object_ptr<td_api::inlineQueryResults> results,
Promise<td_api::object_ptr<td_api::currentWeather>> &&promise) {
TRY_STATUS_PROMISE(promise, G()->close_status());
if (results->results_.size() != 1u || results->results_[0]->get_id() != td_api::inlineQueryResultArticle::ID) {
LOG(ERROR) << "Receive " << to_string(results);
return promise.set_error(Status::Error(500, "Not supported"));
}
auto result = td_api::move_object_as<td_api::inlineQueryResultArticle>(results->results_[0]);
if (!is_emoji(result->title_)) {
LOG(ERROR) << "Receive " << to_string(results);
return promise.set_error(Status::Error(500, "Not supported"));
}
promise.set_value(td_api::make_object<td_api::currentWeather>(to_double(result->description_), result->title_));
}
void InlineQueriesManager::send_inline_query(UserId bot_user_id, DialogId dialog_id, Location user_location,
const string &query, const string &offset,
Promise<td_api::object_ptr<td_api::inlineQueryResults>> &&promise) {
if (td_->auth_manager_->is_bot()) {
return promise.set_error(Status::Error(400, "Bot can't send inline queries to other bot"));
}
CHECK(!td_->auth_manager_->is_bot());
auto r_bot_data = td_->user_manager_->get_bot_data(bot_user_id);
if (r_bot_data.is_error()) {

View File

@ -55,6 +55,8 @@ class InlineQueriesManager final : public Actor {
td_api::object_ptr<td_api::InputInlineQueryResult> &&input_result,
Promise<td_api::object_ptr<td_api::sentWebAppMessage>> &&promise) const;
void get_weather(Location location, Promise<td_api::object_ptr<td_api::currentWeather>> &&promise);
void send_inline_query(UserId bot_user_id, DialogId dialog_id, Location user_location, const string &query,
const string &offset, Promise<td_api::object_ptr<td_api::inlineQueryResults>> &&promise);
@ -106,13 +108,21 @@ class InlineQueriesManager final : public Actor {
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);
bool update_bot_usage(UserId bot_user_id);
void save_recently_used_bots();
bool load_recently_used_bots(Promise<Unit> &promise);
void do_get_weather(DialogId dialog_id, Location location,
Promise<td_api::object_ptr<td_api::currentWeather>> &&promise);
void on_get_weather(td_api::object_ptr<td_api::inlineQueryResults> results,
Promise<td_api::object_ptr<td_api::currentWeather>> &&promise);
td_api::object_ptr<td_api::inlineQueryResults> get_inline_query_results_object(uint64 query_hash);
static void on_drop_inline_query_result_timeout_callback(void *inline_queries_manager_ptr, int64 query_hash);

View File

@ -5935,6 +5935,12 @@ void Td::on_request(uint64 id, td_api::editQuickReplyMessage &request) {
std::move(request.input_message_content_), std::move(promise));
}
void Td::on_request(uint64 id, const td_api::getCurrentWeather &request) {
CHECK_IS_USER();
CREATE_REQUEST_PROMISE();
inline_queries_manager_->get_weather(Location(request.location_), std::move(promise));
}
void Td::on_request(uint64 id, const td_api::getStory &request) {
CHECK_IS_USER();
CREATE_REQUEST_PROMISE();

View File

@ -955,6 +955,8 @@ class Td final : public Actor {
void on_request(uint64 id, td_api::editQuickReplyMessage &request);
void on_request(uint64 id, const td_api::getCurrentWeather &request);
void on_request(uint64 id, const td_api::getStory &request);
void on_request(uint64 id, const td_api::getChatsToSendStories &request);

View File

@ -4605,6 +4605,11 @@ class CliClient final : public Actor {
send_request(td_api::make_object<td_api::setPinnedChats>(as_chat_list(op), as_chat_ids(args)));
} else if (op == "rcl" || op == "rcla" || begins_with(op, "rcl-")) {
send_request(td_api::make_object<td_api::readChatList>(as_chat_list(op)));
} else if (op == "gcwe") {
string latitude;
string longitude;
get_args(args, latitude, longitude);
send_request(td_api::make_object<td_api::getCurrentWeather>(as_location(latitude, longitude, "0.0")));
} else if (op == "gst" || op == "gstl") {
ChatId story_sender_chat_id;
StoryId story_id;