2023-02-07 21:55:46 +03:00
|
|
|
module PublishHelperBot.Handlers
|
|
|
|
|
|
|
|
open System.Threading.Tasks
|
2023-02-11 04:16:34 +03:00
|
|
|
open Microsoft.FSharp.Core
|
2023-02-07 21:55:46 +03:00
|
|
|
open PublishHelperBot.Environment
|
2023-02-11 04:16:34 +03:00
|
|
|
open PublishHelperBot.YoutubeDl
|
2023-02-07 21:55:46 +03:00
|
|
|
open Telegram.Bot
|
|
|
|
open Telegram.Bot.Types
|
|
|
|
open Telegram.Bot.Types.Enums
|
|
|
|
|
|
|
|
type BaseHandlerArgs = Update * BotConfig
|
2023-02-25 00:04:13 +03:00
|
|
|
|
2023-02-07 21:55:46 +03:00
|
|
|
type HandlerArgs = Update * BotConfig * ITelegramBotClient
|
2023-02-25 00:04:13 +03:00
|
|
|
|
2023-02-07 21:55:46 +03:00
|
|
|
type HandlerRequirements = BaseHandlerArgs -> bool
|
2023-02-25 00:04:13 +03:00
|
|
|
|
2023-02-07 21:55:46 +03:00
|
|
|
type Handler = HandlerArgs -> Task
|
2023-02-25 00:04:13 +03:00
|
|
|
|
2023-02-11 04:16:34 +03:00
|
|
|
type Handler<'deps> = 'deps * HandlerArgs -> Task
|
2023-02-07 21:55:46 +03:00
|
|
|
|
2023-02-07 22:10:16 +03:00
|
|
|
// Utils
|
2023-02-07 21:55:46 +03:00
|
|
|
let UpdateIsAMessage (x: Update) = x.Type = UpdateType.Message
|
2023-02-25 00:04:13 +03:00
|
|
|
|
2023-02-07 21:55:46 +03:00
|
|
|
let FromAdminChat (x: Message, c: BotConfig) = x.Chat.Id = c.adminChatId
|
2023-02-25 00:04:13 +03:00
|
|
|
|
|
|
|
let HasReply (x: Message) = not(isNull x.ReplyToMessage)
|
|
|
|
|
2023-02-07 21:55:46 +03:00
|
|
|
let ExtractPhotoFromMessage (x: Message) = Array.map (fun (p: PhotoSize) -> p.FileId) x.Photo
|
2023-02-25 00:04:13 +03:00
|
|
|
|
2023-02-11 07:27:02 +03:00
|
|
|
let HasText (x: Message) = not(isNull x.Text)
|
2023-02-25 00:04:13 +03:00
|
|
|
|
2023-02-07 21:55:46 +03:00
|
|
|
let UrlsAsAlbumInputMedia (urls: string[]): IAlbumInputMedia[] =
|
|
|
|
Array.map (fun (x: string) -> InputMediaPhoto(x)) urls
|
|
|
|
|
|
|
|
// Post (Relay) command
|
2023-02-25 00:04:13 +03:00
|
|
|
type RelayCaptionMode =
|
|
|
|
| WithAuthor
|
|
|
|
| Anonymous
|
|
|
|
| Unknown
|
|
|
|
|
2023-02-07 21:55:46 +03:00
|
|
|
let RelaySupportedContent (x: Message) =
|
2023-02-25 00:04:13 +03:00
|
|
|
match x.Type with
|
|
|
|
| MessageType.Text -> true
|
|
|
|
| MessageType.Photo -> true
|
|
|
|
| MessageType.Video -> true
|
|
|
|
| _ -> false
|
2023-02-07 21:55:46 +03:00
|
|
|
|
|
|
|
let RelayCaptionType (command: string) =
|
|
|
|
match command with
|
|
|
|
| _ when command.StartsWith "\\post anon" -> Anonymous
|
|
|
|
| _ when command.StartsWith "\\post" -> WithAuthor
|
|
|
|
| _ -> Unknown
|
|
|
|
|
2023-02-07 22:15:46 +03:00
|
|
|
let RelayCaption (name: string, url: string) = $"<a href=\"{url}\">Прислал</a> {name}"
|
2023-02-07 21:55:46 +03:00
|
|
|
let RelayParseMode = ParseMode.Html;
|
|
|
|
|
|
|
|
let RelayResolveCaption (mode: RelayCaptionMode, username: string, linkUrl: string) =
|
2023-02-25 00:04:13 +03:00
|
|
|
match mode with
|
|
|
|
| WithAuthor -> RelayCaption(username, linkUrl)
|
|
|
|
| _ -> null
|
2023-02-07 21:55:46 +03:00
|
|
|
|
|
|
|
let public RelayMatch: HandlerRequirements = fun (u, c) ->
|
2023-02-25 00:04:13 +03:00
|
|
|
UpdateIsAMessage u &&
|
|
|
|
FromAdminChat <| (u.Message, c) &&
|
|
|
|
HasReply u.Message &&
|
2023-02-25 00:14:47 +03:00
|
|
|
HasText u.Message &&
|
2023-02-25 00:04:13 +03:00
|
|
|
RelaySupportedContent u.Message.ReplyToMessage &&
|
|
|
|
not (RelayCaptionType u.Message.Text = RelayCaptionMode.Unknown)
|
|
|
|
|
|
|
|
let public RelayHandler: Handler = fun (update, config, tg) ->
|
|
|
|
let reply = update.Message.ReplyToMessage
|
|
|
|
let channelId = config.chanelId
|
|
|
|
let author = $"{reply.From.FirstName} {reply.From.LastName}"
|
|
|
|
let captionMode = RelayCaptionType update.Message.Text
|
|
|
|
|
|
|
|
let photoMedia = lazy Array.get (ExtractPhotoFromMessage reply) 0
|
|
|
|
let caption = lazy RelayResolveCaption(captionMode, author, config.relayUrl)
|
|
|
|
|
|
|
|
match reply.Type with
|
|
|
|
| MessageType.Text -> tg.ForwardMessageAsync(channelId, reply.Chat.Id, reply.MessageId)
|
|
|
|
| MessageType.Photo -> tg.SendPhotoAsync(channelId, photoMedia.Value, caption = caption.Value,
|
|
|
|
parseMode = RelayParseMode)
|
|
|
|
| MessageType.Video -> tg.SendVideoAsync(channelId, reply.Video.FileId, caption = caption.Value,
|
|
|
|
parseMode = RelayParseMode)
|
|
|
|
| _ -> Task.CompletedTask
|
2023-02-07 22:10:16 +03:00
|
|
|
|
|
|
|
|
2023-02-11 04:16:34 +03:00
|
|
|
// YoutubeDL repost
|
2023-02-07 22:10:16 +03:00
|
|
|
|
2023-02-11 04:16:34 +03:00
|
|
|
let YoutubeRepostMatchCmd = "\\ytdl"
|
2023-02-07 22:10:16 +03:00
|
|
|
let public YoutubeRepostMatch: HandlerRequirements = fun (u, c) ->
|
2023-02-25 00:04:13 +03:00
|
|
|
UpdateIsAMessage u &&
|
|
|
|
FromAdminChat <| (u.Message, c) &&
|
|
|
|
HasText <| u.Message &&
|
|
|
|
u.Message.Text.StartsWith YoutubeRepostMatchCmd &&
|
|
|
|
u.Message.Text.Split(' ').Length = 2
|
|
|
|
|
|
|
|
let public YoutubeRepostHandler: Handler<IYoutubeDlService> = fun (yt, (u, c, tg)) ->
|
|
|
|
task {
|
|
|
|
let trim (x: string) = x.Trim()
|
|
|
|
let! id = YoutubeRepostMatchCmd |> u.Message.Text.Split |> Array.last |> trim |> yt.AddJob
|
|
|
|
do! tg.SendTextMessageAsync(c.adminChatId, id.ToString()) |> Async.AwaitTask |> Async.Ignore
|
|
|
|
}
|