support /userlist[@botname] commands in group chat

This commit is contained in:
mappu 2018-06-04 16:54:10 +12:00
parent 3752fce714
commit 8ba57e36e6

View File

@ -24,6 +24,7 @@ const (
type NTFServer struct { type NTFServer struct {
bot *telegram.BotAPI bot *telegram.BotAPI
hubMessages chan upstreamMessage hubMessages chan upstreamMessage
botName string
chatName string chatName string
inviteLink string inviteLink string
configFile 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) log.Printf("Connected to telegram as '%s'", bot.Self.UserName)
ret.botName = bot.Self.UserName
// Groupchat properties // Groupchat properties
if ret.IsSetupMode() { if ret.IsSetupMode() {
@ -434,9 +437,8 @@ func (this *NTFServer) HandleGroupMessage(update telegram.Update) error {
} }
if len(update.Message.Text) > 0 { 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) userID := int64(update.Message.From.ID)
hubNick, ok := this.config.KnownUsers[userID] hubNick, ok := this.config.KnownUsers[userID]
if !ok { if !ok {
@ -448,6 +450,22 @@ 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) return fmt.Errorf("Couldn't send public message for user '%d' (%s) unexpectedly missing upstream connection!", userID, hubNick)
} }
// Intercept some bot commands
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("<i>Can't get userlist for you (internal error)</i>")
}
this.GroupChatSayHTML(fmt.Sprintf("<i>Online users:</i> %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 // 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) this.Coalesce(hubNick, update.Message.Text)
@ -458,6 +476,7 @@ func (this *NTFServer) HandleGroupMessage(update telegram.Update) error {
this.GroupChatSayHTML(fmt.Sprintf("<i>Couldn't sync message '%s' because: %s</i>", html.EscapeString(update.Message.Text), html.EscapeString(err.Error()))) this.GroupChatSayHTML(fmt.Sprintf("<i>Couldn't sync message '%s' because: %s</i>", html.EscapeString(update.Message.Text), html.EscapeString(err.Error())))
} }
} }
}
// TODO probably a file/image upload??? // TODO probably a file/image upload???
// TODO support "editing messages" by re-sending them with a ** suffix // TODO support "editing messages" by re-sending them with a ** suffix
@ -490,6 +509,24 @@ func (this *NTFServer) GroupChatSayHTML(str string) error {
return err 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 { func (this *NTFServer) HandleDirectMessage(update telegram.Update) error {
// Registration workflow // 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)") return respond("Can't get userlist for you (No upstream hub connection)")
} }
usernames := make([]string, 0) usernames, err := this.getUserlistText(conn)
err := conn.Users(func(umap *map[string]libnmdc.UserInfo) error {
for k, _ := range *umap {
usernames = append(usernames, k)
}
return nil
})
if err != nil { if err != nil {
log.Printf("Error retrieving userlist: %s", err.Error())
return respond("Can't get userlist for you (internal error)") return respond("Can't get userlist for you (internal error)")
} }
sort.Strings(usernames) return respond("Online users: " + usernames)
return respond("Online users: " + strings.Join(usernames, " "))
} else if strings.HasPrefix(msg, "/whois ") { } else if strings.HasPrefix(msg, "/whois ") {
target := msg[7:] target := msg[7:]