RocketChat / Rocket.Chat.Go.SDK

Go SDK for REST API and Realtime api
MIT License
60 stars 58 forks source link

SubscribeToMessageStream need help to understand #32

Closed tbellembois closed 5 years ago

tbellembois commented 5 years ago

Hi,

I try to develop a real time light bot for Rocket. My bot subscribe to each direct room (ie. conversation with other users) with a common channel.

    if channels, e = c.GetChannelsIn(); e != nil {
        log.Panic("can not get channels in " + e.Error())
    }

    msgChannel := make(chan models.Message, 10)
    for _, ch := range channels {
        fmt.Println(fmt.Printf("subscribed to: %v", ch.ID))
        if e = c.SubscribeToMessageStream(&models.Channel{ID: ch.ID}, msgChannel); e != nil {
            log.Println("can not subscribe to message stream for channel " + ch.ID + " " + e.Error())
        }
    }

    count := 0
    for {
        fmt.Println(count)
        select {
        case m := <-msgChannel:
            fmt.Println(fmt.Sprintf("ID: %s RoomID: %s Msg: %s User: %v", m.ID, m.RoomID, m.Msg, m.User))
            count++
        }
    }

When I send a direct message to the bot on the Rocket Chat application the for loop prints exactly [number of subscribed channels] times the log message. It can not figure out why. I just wanted to wait for messages in the channel and reply to the sender.

Thanks for your help.

tbellembois commented 5 years ago

Did not see pull request #21 sorry. This fixes my issue.