Add ability to show when user is typing across Slack bridges (#559)

This commit is contained in:
Patrick Connolly 2018-11-09 03:45:40 +08:00 committed by Wim
parent ea86849a58
commit 3d281b3316
5 changed files with 55 additions and 3 deletions

View File

@ -23,6 +23,7 @@ const (
EVENT_USER_ACTION = "user_action" EVENT_USER_ACTION = "user_action"
EVENT_MSG_DELETE = "msg_delete" EVENT_MSG_DELETE = "msg_delete"
EVENT_API_CONNECTED = "api_connected" EVENT_API_CONNECTED = "api_connected"
EVENT_USER_TYPING = "user_typing"
) )
type Message struct { type Message struct {
@ -110,6 +111,7 @@ type Protocol struct {
Server string // IRC,mattermost,XMPP,discord Server string // IRC,mattermost,XMPP,discord
ShowJoinPart bool // all protocols ShowJoinPart bool // all protocols
ShowTopicChange bool // slack ShowTopicChange bool // slack
ShowUserTyping bool // slack
ShowEmbeds bool // discord ShowEmbeds bool // discord
SkipTLSVerify bool // IRC, mattermost SkipTLSVerify bool // IRC, mattermost
StripNick bool // all protocols StripNick bool // all protocols

View File

@ -23,7 +23,9 @@ func (b *Bslack) handleSlack() {
time.Sleep(time.Second) time.Sleep(time.Second)
b.Log.Debug("Start listening for Slack messages") b.Log.Debug("Start listening for Slack messages")
for message := range messages { for message := range messages {
b.Log.Debugf("<= Sending message from %s on %s to gateway", message.Username, b.Account) if message.Event != config.EVENT_USER_TYPING {
b.Log.Debugf("<= Sending message from %s on %s to gateway", message.Username, b.Account)
}
// cleanup the message // cleanup the message
message.Text = b.replaceMention(message.Text) message.Text = b.replaceMention(message.Text)
@ -46,6 +48,14 @@ func (b *Bslack) handleSlackClient(messages chan *config.Message) {
b.Log.Debugf("== Receiving event %#v", msg.Data) b.Log.Debugf("== Receiving event %#v", msg.Data)
} }
switch ev := msg.Data.(type) { switch ev := msg.Data.(type) {
case *slack.UserTypingEvent:
rmsg, err := b.handleTypingEvent(ev)
if err != nil {
b.Log.Errorf("%#v", err)
continue
}
messages <- rmsg
case *slack.MessageEvent: case *slack.MessageEvent:
if b.skipMessageEvent(ev) { if b.skipMessageEvent(ev) {
b.Log.Debugf("Skipped message: %#v", ev) b.Log.Debugf("Skipped message: %#v", ev)
@ -225,6 +235,24 @@ func (b *Bslack) handleAttachments(ev *slack.MessageEvent, rmsg *config.Message)
var commentRE = regexp.MustCompile(`.*?commented: (.*)`) var commentRE = regexp.MustCompile(`.*?commented: (.*)`)
func (b *Bslack) handleTypingEvent(ev *slack.UserTypingEvent) (*config.Message, error) {
var err error
// use our own func because rtm.GetChannelInfo doesn't work for private channels
channelInfo, err := b.getChannelByID(ev.Channel)
if err != nil {
return nil, err
}
rmsg := config.Message{
Channel: channelInfo.Name,
Account: b.Account,
Event: config.EVENT_USER_TYPING,
}
return &rmsg, nil
}
// handleDownloadFile handles file download // handleDownloadFile handles file download
func (b *Bslack) handleDownloadFile(rmsg *config.Message, file *slack.File) error { func (b *Bslack) handleDownloadFile(rmsg *config.Message, file *slack.File) error {
if b.fileIsAvailable(file) { if b.fileIsAvailable(file) {

View File

@ -187,7 +187,10 @@ func (b *Bslack) Reload(cfg *bridge.Config) (string, error) {
} }
func (b *Bslack) Send(msg config.Message) (string, error) { func (b *Bslack) Send(msg config.Message) (string, error) {
b.Log.Debugf("=> Receiving %#v", msg) // Too noisy to log like other events
if msg.Event != config.EVENT_USER_TYPING {
b.Log.Debugf("=> Receiving %#v", msg)
}
// Make a action /me of the message // Make a action /me of the message
if msg.Event == config.EVENT_USER_ACTION { if msg.Event == config.EVENT_USER_ACTION {
@ -266,6 +269,12 @@ func (b *Bslack) sendRTM(msg config.Message) (string, error) {
if err != nil { if err != nil {
return "", fmt.Errorf("could not send message: %v", err) return "", fmt.Errorf("could not send message: %v", err)
} }
if msg.Event == config.EVENT_USER_TYPING {
if b.GetBool("ShowUserTyping") {
b.rtm.SendMessage(b.rtm.NewTypingMessage(channelInfo.ID))
}
return "", nil
}
// Delete message // Delete message
if msg.Event == config.EVENT_MSG_DELETE { if msg.Event == config.EVENT_MSG_DELETE {

View File

@ -297,7 +297,11 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
continue continue
} }
} }
flog.Debugf("=> Sending %#v from %s (%s) to %s (%s)", msg, msg.Account, originchannel, dest.Account, channel.Name)
// Too noisy to log like other events
if msg.Event != config.EVENT_USER_TYPING {
flog.Debugf("=> Sending %#v from %s (%s) to %s (%s)", msg, msg.Account, originchannel, dest.Account, channel.Name)
}
msg.Channel = channel.Name msg.Channel = channel.Name
msg.Avatar = gw.modifyAvatar(origmsg, dest) msg.Avatar = gw.modifyAvatar(origmsg, dest)
@ -337,6 +341,9 @@ func (gw *Gateway) ignoreMessage(msg *config.Message) bool {
// check if we need to ignore a empty message // check if we need to ignore a empty message
if msg.Text == "" { if msg.Text == "" {
if msg.Event == config.EVENT_USER_TYPING {
return false
}
// we have an attachment or actual bytes, do not ignore // we have an attachment or actual bytes, do not ignore
if msg.Extra != nil && if msg.Extra != nil &&
(msg.Extra["attachments"] != nil || (msg.Extra["attachments"] != nil ||

View File

@ -665,6 +665,12 @@ ShowTopicChange=false
#OPTIONAL (default false) #OPTIONAL (default false)
PreserveThreading=false PreserveThreading=false
#Enable showing "user_typing" events from across gateway when available.
#Hint: Set your bot/user's "Full Name" to be "Someone",
#and so the message will say "Someone is typing".
#OPTIONAL (default false)
ShowUserTyping=false
################################################################### ###################################################################
#discord section #discord section
################################################################### ###################################################################