publishhelperbot/PublishHelperBot/Handlers.fs

77 lines
2.8 KiB
Forth
Raw Normal View History

2023-02-07 21:55:46 +03:00
module PublishHelperBot.Handlers
open System.Threading.Tasks
open PublishHelperBot.Environment
open Telegram.Bot
open Telegram.Bot.Types
open Telegram.Bot.Types.Enums
type BaseHandlerArgs = Update * BotConfig
type HandlerArgs = Update * BotConfig * ITelegramBotClient
type HandlerRequirements = BaseHandlerArgs -> bool
type Handler = HandlerArgs -> Task
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
let FromAdminChat (x: Message, c: BotConfig) = x.Chat.Id = c.adminChatId
let HasReply (x: Message) = not(isNull x.ReplyToMessage)
let ExtractPhotoFromMessage (x: Message) = Array.map (fun (p: PhotoSize) -> p.FileId) x.Photo
let UrlsAsAlbumInputMedia (urls: string[]): IAlbumInputMedia[] =
Array.map (fun (x: string) -> InputMediaPhoto(x)) urls
// Post (Relay) command
type RelayCaptionMode = WithAuthor | Anonymous | Unknown
let RelaySupportedContent (x: Message) =
match x.Type with
| MessageType.Text -> true
| MessageType.Photo -> true
| MessageType.Video -> true
| _ -> false
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) =
match mode with
| WithAuthor -> RelayCaption(username, linkUrl)
| _ -> null
let public RelayMatch: HandlerRequirements = fun (u, c) ->
UpdateIsAMessage u &&
FromAdminChat <| (u.Message, c) &&
HasReply u.Message &&
RelaySupportedContent u.Message.ReplyToMessage &&
not (RelayCaptionType u.Message.Text = RelayCaptionMode.Unknown)
let public RelayHandler: Handler = fun (u, c, tg) ->
let reply = u.Message.ReplyToMessage
let channelId = c.chanelId
2023-02-07 22:15:46 +03:00
let author = $"{reply.From.FirstName} {reply.From.LastName}"
2023-02-07 21:55:46 +03:00
let captionMode = RelayCaptionType u.Message.Text
let photoMedia = lazy Array.get (ExtractPhotoFromMessage reply) 0
let caption = lazy RelayResolveCaption(captionMode, author, c.relayUrl)
match reply.Type with
| MessageType.Text -> tg.SendTextMessageAsync(channelId, reply.Text)
| 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
// Youtube repost
let public YoutubeRepostMatch: HandlerRequirements = fun (u, c) ->
UpdateIsAMessage u &&
FromAdminChat <| (u.Message, c) &&
u.Message.Text.StartsWith("\\yt") &&
u.Message.Text.Split(' ').Length = 2