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
1 changed files with 48 additions and 19 deletions

View File

@ -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("<i>Couldn't sync message '%s' because: %s</i>", 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("<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
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("<i>Couldn't sync message '%s' because: %s</i>", 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:]