From 49dfc113c1f458c3872cf35b1045ad1f03f37532 Mon Sep 17 00:00:00 2001 From: gxalpha Date: Sun, 8 May 2022 16:42:20 +0200 Subject: [PATCH] UI: Add SendChatMessage to YouTube API wrappers --- UI/data/locale/en-US.ini | 5 +++++ UI/window-youtube-actions.cpp | 6 ++++-- UI/youtube-api-wrappers.cpp | 24 ++++++++++++++++++++++-- UI/youtube-api-wrappers.hpp | 5 ++++- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/UI/data/locale/en-US.ini b/UI/data/locale/en-US.ini index c1a015603..3513f0f83 100644 --- a/UI/data/locale/en-US.ini +++ b/UI/data/locale/en-US.ini @@ -1326,3 +1326,8 @@ YouTube.Errors.livePermissionBlocked="Live streaming is unavailable on the selec YouTube.Errors.errorExecutingTransition="Transition failed due to a backend error. Please try again in a few seconds." YouTube.Errors.errorStreamInactive="YouTube is not receiving data for your stream. Please check your configuration and try again." YouTube.Errors.invalidTransition="The attempted transition was invalid. This may be due to the stream not having finished a previous transition. Please wait a few seconds and try again." +# Chat errors +YouTube.Errors.liveChatDisabled="Live chat is disabled on this stream." +YouTube.Errors.liveChatEnded="Live stream has ended." +YouTube.Errors.messageTextInvalid="The message text is not valid." +YouTube.Errors.rateLimitExceeded="You are sending messages too quickly." diff --git a/UI/window-youtube-actions.cpp b/UI/window-youtube-actions.cpp index b3805cc5e..c07119c67 100644 --- a/UI/window-youtube-actions.cpp +++ b/UI/window-youtube-actions.cpp @@ -497,7 +497,8 @@ bool OBSYoutubeActions::CreateEventAction(YoutubeApiWrappers *api, blog(LOG_DEBUG, "No stream created."); return false; } - if (!apiYouTube->BindStream(broadcast.id, stream.id)) { + json11::Json json; + if (!apiYouTube->BindStream(broadcast.id, stream.id, json)) { blog(LOG_DEBUG, "No stream binded."); return false; } @@ -547,7 +548,8 @@ bool OBSYoutubeActions::ChooseAnEventAction(YoutubeApiWrappers *api, blog(LOG_DEBUG, "No stream created."); return false; } - if (!apiYouTube->BindStream(selectedBroadcast, stream.id)) { + if (!apiYouTube->BindStream(selectedBroadcast, stream.id, + json)) { blog(LOG_DEBUG, "No stream binded."); return false; } diff --git a/UI/youtube-api-wrappers.cpp b/UI/youtube-api-wrappers.cpp index 6d48513c9..876ecad4f 100644 --- a/UI/youtube-api-wrappers.cpp +++ b/UI/youtube-api-wrappers.cpp @@ -29,6 +29,7 @@ using namespace json11; #define YOUTUBE_LIVE_TOKEN_URL "https://oauth2.googleapis.com/token" #define YOUTUBE_LIVE_VIDEOCATEGORIES_URL YOUTUBE_LIVE_API_URL "/videoCategories" #define YOUTUBE_LIVE_VIDEOS_URL YOUTUBE_LIVE_API_URL "/videos" +#define YOUTUBE_LIVE_CHAT_MESSAGES_URL YOUTUBE_LIVE_API_URL "/liveChat/messages" #define YOUTUBE_LIVE_THUMBNAIL_URL \ "https://www.googleapis.com/upload/youtube/v3/thumbnails/set" @@ -274,7 +275,8 @@ bool YoutubeApiWrappers::InsertStream(StreamDescription &stream) } bool YoutubeApiWrappers::BindStream(const QString broadcast_id, - const QString stream_id) + const QString stream_id, + json11::Json &json_out) { lastErrorMessage.clear(); lastErrorReason.clear(); @@ -285,7 +287,6 @@ bool YoutubeApiWrappers::BindStream(const QString broadcast_id, const QString url = url_template.arg(broadcast_id, stream_id); const Json data = Json::object{}; this->broadcast_id = broadcast_id; - Json json_out; return InsertCommand(QT_TO_UTF8(url), "application/json", "", data.dump().c_str(), json_out); } @@ -583,3 +584,22 @@ bool YoutubeApiWrappers::FindStream(const QString &id, json11::Json &json_out) return true; } + +bool YoutubeApiWrappers::SendChatMessage(const std::string &chat_id, + const QString &message) +{ + QByteArray url = YOUTUBE_LIVE_CHAT_MESSAGES_URL "?part=snippet"; + + json11::Json json_in = Json::object{ + {"snippet", + Json::object{ + {"liveChatId", chat_id}, + {"type", "textMessageEvent"}, + {"textMessageDetails", + Json::object{{"messageText", QT_TO_UTF8(message)}}}, + }}}; + + json11::Json json_out; + return InsertCommand(url, "application/json", "POST", + json_in.dump().c_str(), json_out); +} diff --git a/UI/youtube-api-wrappers.hpp b/UI/youtube-api-wrappers.hpp index 6fc939ee2..dcabc7020 100644 --- a/UI/youtube-api-wrappers.hpp +++ b/UI/youtube-api-wrappers.hpp @@ -57,7 +57,8 @@ public: bool GetChannelDescription(ChannelDescription &channel_description); bool InsertBroadcast(BroadcastDescription &broadcast); bool InsertStream(StreamDescription &stream); - bool BindStream(const QString broadcast_id, const QString stream_id); + bool BindStream(const QString broadcast_id, const QString stream_id, + json11::Json &json_out); bool GetBroadcastsList(json11::Json &json_out, const QString &page, const QString &status); bool @@ -74,6 +75,8 @@ public: json11::Json &json_out); bool StartLatestBroadcast(); bool StopLatestBroadcast(); + bool SendChatMessage(const std::string &chat_id, + const QString &message); void SetBroadcastId(QString &broadcast_id); QString GetBroadcastId();