From 2374d39b41a5ee7e1bcd4bd342b4f44511e8ce9a Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Tue, 26 Dec 2023 19:37:34 +0800 Subject: [PATCH] add replyTo support for /get --- project/MornyConfiguration.scala | 2 +- .../cono/morny/social_share/command/GetSocial.scala | 12 ++++++++++-- .../cono/morny/social_share/event/OnGetSocial.scala | 10 ++++------ .../cono/morny/util/tgapi/TelegramExtensions.scala | 4 ++++ 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/project/MornyConfiguration.scala b/project/MornyConfiguration.scala index 772a4d1..d5ba8f0 100644 --- a/project/MornyConfiguration.scala +++ b/project/MornyConfiguration.scala @@ -8,7 +8,7 @@ object MornyConfiguration { val MORNY_CODE_STORE = "https://github.com/Eyre-S/Coeur-Morny-Cono" val MORNY_COMMIT_PATH = "https://github.com/Eyre-S/Coeur-Morny-Cono/commit/%s" - val VERSION = "2.0.0-alpha8" + val VERSION = "2.0.0-alpha9" val VERSION_DELTA: Option[String] = None val CODENAME = "guanggu" diff --git a/src/main/scala/cc/sukazyo/cono/morny/social_share/command/GetSocial.scala b/src/main/scala/cc/sukazyo/cono/morny/social_share/command/GetSocial.scala index 66147eb..b540805 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/social_share/command/GetSocial.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/social_share/command/GetSocial.scala @@ -24,9 +24,17 @@ class GetSocial (using coeur: MornyCoeur) extends ITelegramCommand { TelegramStickers.ID_404 ).replyToMessageId(event.message.messageId()) - if command.args.length < 1 then { do404(); return } + val content = + if command.args.length > 0 then + Right(command.args(0)) + else if event.message.replyToMessage != null then + import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Message.textWithUrls + Left(event.message.replyToMessage.textWithUrls) + else + do404() + return - if !OnGetSocial.tryFetchSocial(Right(command.args(0)))(using event.message.chat.id, event.message.messageId) then + if !OnGetSocial.tryFetchSocial(content)(using event.message.chat.id, event.message.messageId) then do404() } diff --git a/src/main/scala/cc/sukazyo/cono/morny/social_share/event/OnGetSocial.scala b/src/main/scala/cc/sukazyo/cono/morny/social_share/event/OnGetSocial.scala index 726fe21..8472b31 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/social_share/event/OnGetSocial.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/social_share/event/OnGetSocial.scala @@ -9,7 +9,7 @@ import cc.sukazyo.cono.morny.Log.{exceptionLog, logger} import cc.sukazyo.cono.morny.reporter.MornyReport import cc.sukazyo.cono.morny.social_share.api.{SocialTwitterParser, SocialWeiboParser} import cc.sukazyo.cono.morny.social_share.external.{twitter, weibo} -import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Message.entitiesSafe +import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Message.textWithUrls import com.pengrad.telegrambot.model.Chat import com.pengrad.telegrambot.model.request.ParseMode import com.pengrad.telegrambot.request.{SendMessage, SendSticker} @@ -23,9 +23,7 @@ class OnGetSocial (using coeur: MornyCoeur) extends EventListener { if messageEvent.text == null then return; if tryFetchSocial( - Left(( - messageEvent.text :: messageEvent.entitiesSafe.map(f => f.url).filterNot(f => f == null) - ).mkString(" ")) + Left(messageEvent.textWithUrls) )(using messageEvent.chat.id, messageEvent.messageId) then event.setEventOk @@ -74,7 +72,7 @@ object OnGetSocial { } - def tryFetchSocialOfTweet (url: twitter.TweetUrlInformation)(using replyChat: Long, replyToMessage: Int)(using coeur: MornyCoeur) = + def tryFetchSocialOfTweet (url: twitter.TweetUrlInformation)(using replyChat: Long, replyToMessage: Int)(using coeur: MornyCoeur): Unit = import cc.sukazyo.cono.morny.social_share.external.twitter.FXApi import io.circe.{DecodingFailure, ParsingFailure} import sttp.client3.SttpClientException @@ -90,7 +88,7 @@ object OnGetSocial { "Error on requesting FixTweet API\n" + exceptionLog(e) coeur.externalContext.consume[MornyReport](_.exception(e, "Error on requesting FixTweet API")) - def tryFetchSocialOfWeibo (url: weibo.StatusUrlInfo)(using replyChat: Long, replyToMessage: Int)(using coeur: MornyCoeur) = + def tryFetchSocialOfWeibo (url: weibo.StatusUrlInfo)(using replyChat: Long, replyToMessage: Int)(using coeur: MornyCoeur): Unit = import cc.sukazyo.cono.morny.social_share.external.weibo.MApi import io.circe.{DecodingFailure, ParsingFailure} import sttp.client3.{HttpError, SttpClientException} diff --git a/src/main/scala/cc/sukazyo/cono/morny/util/tgapi/TelegramExtensions.scala b/src/main/scala/cc/sukazyo/cono/morny/util/tgapi/TelegramExtensions.scala index d8811b0..1a8da95 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/util/tgapi/TelegramExtensions.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/util/tgapi/TelegramExtensions.scala @@ -72,6 +72,10 @@ object TelegramExtensions { if self.entities == null then Nil else self.entities.toList + def textWithUrls: String = + (self.text :: self.entitiesSafe.map(_.url).filterNot(_ == null)) + .mkString(" ") + }} class LimboUser (id: Long) extends User(id)