support /userlist[@botname] commands in group chat
This commit is contained in:
parent
3752fce714
commit
8ba57e36e6
67
NTFServer.go
67
NTFServer.go
@ -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,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)
|
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
|
// Intercept some bot commands
|
||||||
this.Coalesce(hubNick, update.Message.Text)
|
|
||||||
|
|
||||||
// Submit to NMDC
|
if update.Message.Text == "/userlist" || update.Message.Text == "/userlist@"+this.botName {
|
||||||
err := conn.SayPublic(update.Message.Text)
|
// Display native userlist inside the groupchat
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to deliver message '%s': %s", update.Message.Text, err.Error())
|
usernames, err := this.getUserlistText(conn)
|
||||||
this.GroupChatSayHTML(fmt.Sprintf("<i>Couldn't sync message '%s' because: %s</i>", html.EscapeString(update.Message.Text), html.EscapeString(err.Error())))
|
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
|
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:]
|
||||||
|
Loading…
Reference in New Issue
Block a user