Skip to content

Commit

Permalink
Add logging for Telegram messages (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Aug 14, 2019
1 parent a02f92f commit 8239d4e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Emulsion/Telegram/Client.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Client(ctx: ServiceContext, cancellationToken: CancellationToken, settings:
inherit MessageSystemBase(ctx, cancellationToken)

override __.RunUntilError receiver =
async { Funogram.run settings cancellationToken receiver }
async { Funogram.run ctx.Logger settings cancellationToken receiver }

override __.Send message =
Funogram.send ctx.Logger settings message
17 changes: 10 additions & 7 deletions Emulsion/Telegram/Funogram.fs
Original file line number Diff line number Diff line change
Expand Up @@ -134,28 +134,31 @@ let private processResultWithValue (logger: ILogger) (result: Result<'a, ApiResp
let private processResult logger (result: Result<'a, ApiResponseError>) =
processResultWithValue logger result |> ignore

let private updateArrived onMessage (ctx : UpdateContext) =
let private updateArrived (logger: ILogger) onMessage (ctx: UpdateContext) =
processCommands ctx [
fun (msg, _) -> onMessage (TelegramMessage (MessageConverter.read msg)); true
fun (msg, _) ->
logger.Information("Incoming Telegram message: {Message}", msg)
onMessage (TelegramMessage(MessageConverter.read msg)); true
] |> ignore

let internal prepareHtmlMessage { author = author; text = text } : string =
let internal prepareHtmlMessage { author = author; text = text }: string =
sprintf "<b>%s</b>\n%s" (Html.escape author) (Html.escape text)

let send (logger: ILogger) (settings: TelegramSettings) (OutgoingMessage content): Async<unit> =
let sendHtmlMessage groupId text =
sendMessageBase groupId text (Some ParseMode.HTML) None None None None

let groupId = Int (int64 settings.GroupId)
let groupId = Int(int64 settings.GroupId)
let message = prepareHtmlMessage content
async {
let! result = api settings.Token (sendHtmlMessage groupId message)
return processResult logger result
}

let run (settings: TelegramSettings)
let run (logger: ILogger)
(settings: TelegramSettings)
(cancellationToken: CancellationToken)
(onMessage: IncomingMessage -> unit) : unit =
(onMessage: IncomingMessage -> unit): unit =
// TODO[F]: Update Funogram and don't ignore the cancellation token here.
let config = { defaultConfig with Token = settings.Token }
Bot.startBot config (updateArrived onMessage) None
Bot.startBot config (updateArrived logger onMessage) None

0 comments on commit 8239d4e

Please sign in to comment.