From 8ba57e36e643fbc48ea323dc1d41b54d6d3858f0 Mon Sep 17 00:00:00 2001 From: mappu Date: Mon, 4 Jun 2018 16:54:10 +1200 Subject: [PATCH] support /userlist[@botname] commands in group chat --- NTFServer.go | 67 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 19 deletions(-) diff --git a/NTFServer.go b/NTFServer.go index cf9d20c..c56062a 100644 --- a/NTFServer.go +++ b/NTFServer.go @@ -24,6 +24,7 @@ const ( type NTFServer struct { bot *telegram.BotAPI hubMessages chan upstreamMessage + botName string chatName string inviteLink string configFile string @@ -86,6 +87,8 @@ func NewNTFServer(configFile string, verbose bool) (*NTFServer, error) { log.Printf("Connected to telegram as '%s'", bot.Self.UserName) + ret.botName = bot.Self.UserName + // Groupchat properties if ret.IsSetupMode() { @@ -434,9 +437,8 @@ func (this *NTFServer) HandleGroupMessage(update telegram.Update) error { } if len(update.Message.Text) > 0 { - // Actual chat message - // Find the responsible user to send it to the upstream hub, using their account + // Find the responsible user's upstream connection userID := int64(update.Message.From.ID) hubNick, ok := this.config.KnownUsers[userID] if !ok { @@ -448,14 +450,31 @@ func (this *NTFServer) HandleGroupMessage(update telegram.Update) error { return fmt.Errorf("Couldn't send public message for user '%d' (%s) unexpectedly missing upstream connection!", userID, hubNick) } - // Also add it to the coalesce buffer so that we don't replay it from someone else's NMDC connection - this.Coalesce(hubNick, update.Message.Text) + // Intercept some bot commands - // Submit to NMDC - err := conn.SayPublic(update.Message.Text) - if err != nil { - log.Printf("Failed to deliver message '%s': %s", update.Message.Text, err.Error()) - this.GroupChatSayHTML(fmt.Sprintf("Couldn't sync message '%s' because: %s", html.EscapeString(update.Message.Text), html.EscapeString(err.Error()))) + if update.Message.Text == "/userlist" || update.Message.Text == "/userlist@"+this.botName { + // Display native userlist inside the groupchat + + usernames, err := this.getUserlistText(conn) + if err != nil { + this.GroupChatSayHTML("Can't get userlist for you (internal error)") + } + + this.GroupChatSayHTML(fmt.Sprintf("Online users: %s", html.EscapeString(usernames))) + + } else { + + // Actual chat message + + // Also add it to the coalesce buffer so that we don't replay it from someone else's NMDC connection + this.Coalesce(hubNick, update.Message.Text) + + // Submit to NMDC + err := conn.SayPublic(update.Message.Text) + if err != nil { + log.Printf("Failed to deliver message '%s': %s", update.Message.Text, err.Error()) + this.GroupChatSayHTML(fmt.Sprintf("Couldn't sync message '%s' because: %s", html.EscapeString(update.Message.Text), html.EscapeString(err.Error()))) + } } } @@ -490,6 +509,24 @@ func (this *NTFServer) GroupChatSayHTML(str string) error { return err } +func (this *NTFServer) getUserlistText(conn *libnmdc.HubConnection) (string, error) { + usernames := make([]string, 0) + err := conn.Users(func(umap *map[string]libnmdc.UserInfo) error { + for k, _ := range *umap { + usernames = append(usernames, k) + } + return nil + }) + + if err != nil { + log.Printf("Error retrieving userlist: %s", err.Error()) + return "", err + } + + sort.Strings(usernames) + return strings.Join(usernames, " "), nil +} + func (this *NTFServer) HandleDirectMessage(update telegram.Update) error { // Registration workflow @@ -576,20 +613,12 @@ func (this *NTFServer) HandleDirectMessage(update telegram.Update) error { return respond("Can't get userlist for you (No upstream hub connection)") } - usernames := make([]string, 0) - err := conn.Users(func(umap *map[string]libnmdc.UserInfo) error { - for k, _ := range *umap { - usernames = append(usernames, k) - } - return nil - }) + usernames, err := this.getUserlistText(conn) if err != nil { - log.Printf("Error retrieving userlist: %s", err.Error()) return respond("Can't get userlist for you (internal error)") } - sort.Strings(usernames) - return respond("Online users: " + strings.Join(usernames, " ")) + return respond("Online users: " + usernames) } else if strings.HasPrefix(msg, "/whois ") { target := msg[7:]