I am trying to create a bot and retrieve the list of channels.
I used the bot example in repository and it is mostly working, except for the part where it has to get the list of channels.
Either I am doing something silly or GetChannels API really does not work the way it is described in bot_sample.go .
I made a smaller separate function to test that part.
Adding code here for better readability:
func mattermostPrintChannels(client *mattermost.Client) {
channelsResult, err := client.GetChannels("")
if err != nil {
fmt.Print("Couldn't get channels: ", err)
return
}
channelList := channelsResult.Data.(*mattermost.ChannelList)
fmt.Print("Channels:")
for _, channel := range channelList.Channels {
fmt.Printf("%s -> %s", channel.Id, channel.DisplayName)
}
}
This code gives me the error:
./mattermost.go:30: channelList.Channels undefined (type *model.ChannelList has no field or method Channels)
Now if I just print the contents of ChannelList variable (using spew), I get the following:
I added prints in that function and I can see that data going into function as parameter data is correct. I can see that it contains the channel info which I was expecting.
However, the json.Decoder call returns nil. .
With this I believe that the channelList declaration is not correct which is leading to incorrect decoding. type ChannelList []*Channel
at line 11
The bot sample needs to be updated for the upcoming 3.5 release since a few of the data structures changed when we were working on improving client performance. We’ll work on an update for the sample on GitHub to smooth out the transition, but in the meantime, here’s the lines of bot_sample.go that need to be changed.
Line 159 needs to changed because model.ChannelList is now just []model.Channel instead of a struct
// Line 159
- for _, channel := range channelList.Channels {
+ for _, channel := range *channelList {
And then lines 203 and 219 need to be changed because model.WebSocketEvent has changed slightly
// Line 203
- if event.ChannelId != debuggingChannel.Id {
+ if event.Broadcast.ChannelId != debuggingChannel.Id {
// Line 219
- if event.UserId == botUser.Id {
+ if event.Broadcast.UserId == botUser.Id {