Skip to content

Commit

Permalink
enhance(telegram): make channel mentions work
Browse files Browse the repository at this point in the history
  • Loading branch information
wass3r committed Nov 15, 2020
1 parent 65ce0f3 commit f3ac4ac
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 7 deletions.
49 changes: 42 additions & 7 deletions remote/telegram/remote.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package telegram

import (
"fmt"
"strconv"
"strings"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/target/flottbot/models"
Expand Down Expand Up @@ -52,19 +54,52 @@ func (c *Client) Read(inputMsgs chan<- models.Message, rules map[string]models.R
updates, err := telegramAPI.GetUpdatesChan(u)

for update := range updates {
if update.Message == nil { // ignore any non-Message Updates
var m *tgbotapi.Message

if update.Message != nil {
m = update.Message
}

if update.ChannelPost != nil {
m = update.ChannelPost
}

if m == nil {
continue
}

msg, mentioned := processMessageText(m.Text, bot.Name)

// support slash commands
if len(m.Command()) > 0 {
fullCmd := fmt.Sprintf("%s %s", m.Command(), m.CommandArguments())
mentioned = true
msg = strings.TrimSpace(fullCmd)
}

message := models.NewMessage()
message.Timestamp = strconv.FormatInt(update.Message.Time().Unix(), 10)
message.Type = models.MsgTypeDirect
message.Input = update.Message.Text
message.Timestamp = strconv.FormatInt(m.Time().Unix(), 10)
message.Type = mapMessageType(*m)
message.Input = msg
message.Output = ""
message.ID = strconv.Itoa(update.Message.MessageID)
message.ID = strconv.Itoa(m.MessageID)
message.Service = models.MsgServiceChat
message.ChannelID = strconv.FormatInt(update.Message.Chat.ID, 10)
message.Vars["_user.name"] = update.Message.Chat.UserName
message.BotMentioned = mentioned
message.ChannelID = strconv.FormatInt(m.Chat.ID, 10)

// populate message with metadata
if m.From != nil {
message.Vars["_user.name"] = m.From.UserName
message.Vars["_user.firstname"] = m.From.FirstName
message.Vars["_user.lastname"] = m.From.LastName
message.Vars["_user.id"] = strconv.Itoa(m.From.ID)
message.Vars["_user.realnamenormalized"] = fmt.Sprintf("%s %s", m.From.FirstName, m.From.LastName)
message.Vars["_user.displayname"] = m.From.UserName
message.Vars["_user.displaynamenormalized"] = m.From.UserName
}

message.Vars["_channel.id"] = message.ChannelID
message.Vars["_channel.name"] = m.Chat.Title

inputMsgs <- message
}
Expand Down
38 changes: 38 additions & 0 deletions remote/telegram/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package telegram

import (
"fmt"
"strings"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/target/flottbot/models"
)

// processMessageText removes existing bot mention
// and returns message and whether bot was mentioned
func processMessageText(m, b string) (string, bool) {
botMention := fmt.Sprintf("@%s", b)
isMentioned := strings.HasPrefix(m, botMention)
message := strings.TrimPrefix(m, botMention)

return strings.TrimSpace(message), isMentioned
}

// mapMessageType converts the type returned for a
// telegram message to an internal, equivalent type
func mapMessageType(t tgbotapi.Message) models.MessageType {
msgType := models.MsgTypeUnknown

switch t.Chat.Type {
case "private":
msgType = models.MsgTypeDirect
case "privatechannel":
msgType = models.MsgTypePrivateChannel
case "channel":
case "group":
case "supergroup":
msgType = models.MsgTypeChannel
}

return msgType
}

0 comments on commit f3ac4ac

Please sign in to comment.