Skip to content

Commit

Permalink
Add support for Telegram forwarded messages (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Aug 18, 2019
1 parent 07bddaa commit ded58ed
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
17 changes: 17 additions & 0 deletions Emulsion.Tests/Telegram/FunogramTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ let private createReplyMessage from text replyTo : Funogram.Types.Message =
{ createMessage from text with
ReplyToMessage = (Some replyTo) }

let private createForwardedMessage from (forwarded: Funogram.Types.Message) =
{ defaultMessage with
From = Some from
ForwardFrom = forwarded.From
Text = forwarded.Text }

let private telegramMessage author text =
{ main = { author = author; text = text }; replyTo = None }

Expand All @@ -31,6 +37,7 @@ let private telegramReplyMessage author text replyTo =

let private originalUser = createUser (Some "originalUser") "" None
let private replyingUser = createUser (Some "replyingUser") "" None
let private forwardingUser = createUser (Some "forwardingUser") "" None

module ReadMessageTests =
let readMessage = Funogram.MessageConverter.read
Expand Down Expand Up @@ -91,6 +98,16 @@ module ReadMessageTests =
readMessage replyMessage
)

[<Fact>]
let forward(): unit =
let forwardedMessage = createMessage (Some originalUser) (Some "test")
let message = createForwardedMessage forwardingUser forwardedMessage

Assert.Equal(
telegramMessage "@forwardingUser" ">> <@originalUser> test",
readMessage message
)

module FlattenMessageTests =
let private flattenMessage = Funogram.MessageConverter.flatten Funogram.MessageConverter.DefaultQuoteSettings
let private flattenMessageLineLimit limit =
Expand Down
34 changes: 19 additions & 15 deletions Emulsion/Telegram/Funogram.fs
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,25 @@ module MessageConverter =
dataRedactedMessage = ""
}

let private getAuthorName (message: FunogramMessage) =
match message.From with
| None -> "[UNKNOWN USER]"
| Some user ->
match user.Username with
| Some username -> sprintf "@%s" username
| None ->
match user.LastName with
| Some lastName -> sprintf "%s %s" user.FirstName lastName
| None -> user.FirstName
let private getUserDisplayName: User option -> string = function
| None -> "[UNKNOWN USER]"
| Some user ->
match user.Username with
| Some username -> sprintf "@%s" username
| None ->
match user.LastName with
| Some lastName -> sprintf "%s %s" user.FirstName lastName
| None -> user.FirstName

let private getMessageBodyText (message: FunogramMessage) =
match message.Text with
| None -> "[DATA UNRECOGNIZED]"
| Some text -> text
let text =
match message.Text with
| None -> "[DATA UNRECOGNIZED]"
| Some text -> text

if Option.isSome message.ForwardFrom
then sprintf "%s<%s> %s" DefaultQuoteSettings.linePrefix (getUserDisplayName message.ForwardFrom) text
else text

let private applyLimits limits text =
let applyMessageLengthLimit (original: {| text: string; wasLimited: bool |}) =
Expand Down Expand Up @@ -114,14 +118,14 @@ module MessageConverter =
{ author = author; text = text }

let internal read (message: FunogramMessage): TelegramMessage =
let mainAuthor = getAuthorName message
let mainAuthor = getUserDisplayName message.From
let mainBody = getMessageBodyText message
let mainMessage = { author = mainAuthor; text = mainBody }

match message.ReplyToMessage with
| None -> { main = mainMessage; replyTo = None }
| Some replyTo ->
let replyToMessage = { author = getAuthorName replyTo; text = getMessageBodyText replyTo }
let replyToMessage = { author = getUserDisplayName replyTo.From; text = getMessageBodyText replyTo }
{ main = mainMessage; replyTo = Some replyToMessage }

let private processResultWithValue (logger: ILogger) (result: Result<'a, ApiResponseError>) =
Expand Down

0 comments on commit ded58ed

Please sign in to comment.