support /userlist[@botname] commands in group chat
This commit is contained in:
parent
3752fce714
commit
8ba57e36e6
53
NTFServer.go
53
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,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)
|
||||
}
|
||||
|
||||
// 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
|
||||
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())))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO probably a file/image upload???
|
||||
// TODO support "editing messages" by re-sending them with a ** suffix
|
||||
@ -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:]
|
||||
|
Loading…
Reference in New Issue
Block a user