From 1230b9e6b58e2f5c5acb5d5c2c2f9d66ad474063 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Wed, 21 Aug 2024 19:08:09 +0800 Subject: [PATCH] inline get social content now can search all available links in query texts --- .../morny/bot/event/MornyOnInlineQuery.scala | 4 ++ .../cono/morny/bot/event/OnGetSocial.scala | 4 +- .../morny/bot/query/InlineQueryUnit.scala | 2 +- .../cono/morny/bot/query/MornyQueries.scala | 1 + .../bot/query/ShareToolSocialContent.scala | 55 +++++++++---------- .../morny/bot/query/ShareToolTwitter.scala | 6 +- .../morny/data/social/SocialContent.scala | 10 ++-- 7 files changed, 42 insertions(+), 40 deletions(-) diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyOnInlineQuery.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyOnInlineQuery.scala index e079dd0..c785528 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyOnInlineQuery.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/event/MornyOnInlineQuery.scala @@ -4,6 +4,8 @@ import cc.sukazyo.cono.morny.MornyCoeur import cc.sukazyo.cono.morny.bot.api.{EventEnv, EventListener} import cc.sukazyo.cono.morny.bot.query.{InlineQueryUnit, MornyQueries} import cc.sukazyo.cono.morny.util.tgapi.TelegramExtensions.Bot.exec +import cc.sukazyo.cono.morny.Log.logger +import com.google.gson.Gson import com.pengrad.telegrambot.model.Update import com.pengrad.telegrambot.model.request.InlineQueryResult import com.pengrad.telegrambot.request.AnswerInlineQuery @@ -30,6 +32,8 @@ class MornyOnInlineQuery (using queryManager: MornyQueries) (using coeur: MornyC if (results isEmpty) return; + logger debug resultAnswers.map(Gson().toJson(_)).mkString("\n") + coeur.account exec AnswerInlineQuery( update.inlineQuery.id, resultAnswers toArray:_* ).cacheTime(cacheTime).isPersonal(isPersonal) diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnGetSocial.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnGetSocial.scala index 5732d58..ba1c095 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnGetSocial.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/event/OnGetSocial.scala @@ -73,7 +73,7 @@ object OnGetSocial { } - def tryFetchSocialOfTweet (url: twitter.TweetUrlInformation)(using replyChat: Long, replyToMessage: Int)(using coeur: MornyCoeur) = + private def tryFetchSocialOfTweet (url: twitter.TweetUrlInformation)(using replyChat: Long, replyToMessage: Int)(using coeur: MornyCoeur) = import io.circe.{DecodingFailure, ParsingFailure} import sttp.client3.SttpClientException import twitter.FXApi @@ -89,7 +89,7 @@ object OnGetSocial { "Error on requesting FixTweet API\n" + exceptionLog(e) coeur.daemons.reporter.exception(e, "Error on requesting FixTweet API") - def tryFetchSocialOfWeibo (url: weibo.StatusUrlInfo)(using replyChat: Long, replyToMessage: Int)(using coeur: MornyCoeur) = + private def tryFetchSocialOfWeibo (url: weibo.StatusUrlInfo)(using replyChat: Long, replyToMessage: Int)(using coeur: MornyCoeur) = import io.circe.{DecodingFailure, ParsingFailure} import sttp.client3.{HttpError, SttpClientException} import weibo.MApi diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/query/InlineQueryUnit.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/query/InlineQueryUnit.scala index b15f4b6..911a5ae 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/query/InlineQueryUnit.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/query/InlineQueryUnit.scala @@ -6,7 +6,7 @@ import com.pengrad.telegrambot.model.request.InlineQueryResult object InlineQueryUnit { object defaults: - val CACHE_TIME = 300 + val CACHE_TIME = 1 val IS_PERSONAL = false } diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/query/MornyQueries.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/query/MornyQueries.scala index d01ed62..3981e0f 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/query/MornyQueries.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/query/MornyQueries.scala @@ -1,6 +1,7 @@ package cc.sukazyo.cono.morny.bot.query import cc.sukazyo.cono.morny.bot.query +import cc.sukazyo.cono.morny.Log.logger import cc.sukazyo.cono.morny.MornyCoeur import com.pengrad.telegrambot.model.Update diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/query/ShareToolSocialContent.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/query/ShareToolSocialContent.scala index 60912e7..6c2ca9c 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/query/ShareToolSocialContent.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/query/ShareToolSocialContent.scala @@ -1,43 +1,40 @@ package cc.sukazyo.cono.morny.bot.query + import cc.sukazyo.cono.morny.data.social.{SocialTwitterParser, SocialWeiboParser} import cc.sukazyo.cono.morny.extra.{twitter, weibo} -import cc.sukazyo.cono.morny.extra.twitter.{FXApi, TweetUrlInformation} -import cc.sukazyo.cono.morny.extra.weibo.{MApi, StatusUrlInfo} +import cc.sukazyo.cono.morny.extra.twitter.FXApi +import cc.sukazyo.cono.morny.extra.weibo.MApi import com.pengrad.telegrambot.model.Update class ShareToolSocialContent extends ITelegramQuery { override def query (event: Update): List[InlineQueryUnit[_]] | Null = { - val _queryRaw = event.inlineQuery.query - val query = - _queryRaw.trim match - case _startsWithTag if _startsWithTag startsWith "get " => - (_startsWithTag drop 4)trim - case _endsWithTag if _endsWithTag endsWith " get" => - (_endsWithTag dropRight 4)trim - case _ => return null + val query = event.inlineQuery.query + if query == null then return null - ( - twitter.parseTweetUrl(query) match - case Some(TweetUrlInformation(_, statusPath, _, statusId, _, _)) => - SocialTwitterParser.parseFXTweet(FXApi.Fetch.status(Some(statusPath), statusId)) - .genInlineQueryResults(using - "morny/share/tweet/content", statusId, - "Twitter Tweet Content" - ) - case None => Nil - ) ::: ( - weibo.parseWeiboStatusUrl(query) match - case Some(StatusUrlInfo(_, id)) => - SocialWeiboParser.parseMStatus(MApi.Fetch.statuses_show(id)) - .genInlineQueryResults(using - "morny/share/weibo/status/content", id, - "Weibo Content" - ) - case None => Nil - ) ::: Nil + twitterTweets(query) ::: weiboStatus(query) } + private def twitterTweets (query: String): List[InlineQueryUnit[_]] = { + twitter.guessTweetUrl(query).flatMap { tweet => + SocialTwitterParser.parseFXTweet(FXApi.Fetch.status(Some(tweet.statusPath), tweet.statusId)) + .genInlineQueryResults(using + "morny/share/tweet/content", tweet.statusId, + "Twitter Tweet Content" + ) + } + } + + private def weiboStatus (query: String): List[InlineQueryUnit[_]] = { + weibo.guessWeiboStatusUrl(query).flatMap { status => + SocialWeiboParser.parseMStatus(MApi.Fetch.statuses_show(status.id)) + .genInlineQueryResults(using + "morny/share/weibo/status/content", status.id, + "Weibo Content" + ) + } + } + } diff --git a/src/main/scala/cc/sukazyo/cono/morny/bot/query/ShareToolTwitter.scala b/src/main/scala/cc/sukazyo/cono/morny/bot/query/ShareToolTwitter.scala index be1fb13..e3afdc7 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/bot/query/ShareToolTwitter.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/bot/query/ShareToolTwitter.scala @@ -2,19 +2,17 @@ package cc.sukazyo.cono.morny.bot.query import cc.sukazyo.cono.morny.extra.twitter import cc.sukazyo.cono.morny.extra.twitter.TweetUrlInformation -import cc.sukazyo.cono.morny.util.tgapi.formatting.NamingUtils.inlineQueryId import com.pengrad.telegrambot.model.Update import com.pengrad.telegrambot.model.request.InlineQueryResultArticle import scala.language.postfixOps -import scala.util.matching.Regex class ShareToolTwitter extends ITelegramQuery { private val TITLE_VX = "[tweet] Share as VxTwitter" - private val ID_PREFIX_VX = "[morny/share/twitter/vxtwi]" + private val ID_PREFIX_VX = "[morny/share/twitter/vx_url]" private val TITLE_FX = "[tweet] Share as Fix-Tweet" - private val ID_PREFIX_FX = "[morny/share/twitter/fxtwi]" + private val ID_PREFIX_FX = "[morny/share/twitter/fx_url]" override def query (event: Update): List[InlineQueryUnit[_]] | Null = { diff --git a/src/main/scala/cc/sukazyo/cono/morny/data/social/SocialContent.scala b/src/main/scala/cc/sukazyo/cono/morny/data/social/SocialContent.scala index e7766bc..0c153d2 100644 --- a/src/main/scala/cc/sukazyo/cono/morny/data/social/SocialContent.scala +++ b/src/main/scala/cc/sukazyo/cono/morny/data/social/SocialContent.scala @@ -62,7 +62,7 @@ case class SocialContent ( ( if (medias_mosaic nonEmpty) && (medias_mosaic.get.t == Photo) && medias_mosaic.get.isInstanceOf[SocialMediaWithUrl] then InlineQueryUnit(InlineQueryResultPhoto( - inlineQueryId(s"[$id_head/photo/mosaic]$id_param"), + s"[$id_head/photo/mosaic]$id_param", medias_mosaic.get.asInstanceOf[SocialMediaWithUrl].url, thumbnailOrElse(medias_mosaic.get.asInstanceOf[SocialMediaWithUrl].url) ).title(s"$name").caption(text_html).parseMode(ParseMode.HTML)) :: Nil @@ -71,18 +71,20 @@ case class SocialContent ( media match case media_url: SocialMediaWithUrl => InlineQueryUnit(InlineQueryResultPhoto( - inlineQueryId(s"[$id_head/photo/0]$id_param"), + s"[$id_head/photo/0]$id_param", media_url.url, thumbnailOrElse(media_url.url) ).title(s"$name").caption(text_html).parseMode(ParseMode.HTML)) :: Nil case _ => InlineQueryUnit(InlineQueryResultArticle( - inlineQueryId(s"[$id_head/text_only]$id_param"), s"$name (text only)", + s"[$id_head/text_only]$id_param", + s"$name (text only)", InputTextMessageContent(text_withPicPlaceholder).parseMode(ParseMode.HTML) )) :: Nil else InlineQueryUnit(InlineQueryResultArticle( - inlineQueryId(s"[$id_head/text]$id_param"), s"$name", + s"[$id_head/text]$id_param", + s"$name", InputTextMessageContent(text_html).parseMode(ParseMode.HTML) )) :: Nil ) ::: Nil