GetChannels API fails in golang bot example

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:

channelList:  :  ([]interface {}) (len=1 cap=1) {
 (*model.ChannelList)(<nil>)
}

I have narrowed it down further.
The problem is with the ChannelListFromJson() function.
https://github.com/mattermost/platform/blob/master/model/channel_list.go#L43

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.

<snip>
(*http.bodyEOFSignal)(0xc820187780)({
 body: (*http.body)(0xc820187740)({
  src: (*io.LimitedReader)(0xc8201e64e0)({
   R: (*bufio.Reader)(0xc8201e4360)({
    buf: ([]uint8) (len=4096 cap=4096) {
     00000000  48 54 54 50 2f 31 2e 31  20 32 30 30 20 4f 4b 0d  |HTTP/1.1 200 OK.|
     00000010  0a 53 65 72 76 65 72 3a  20 6e 67 69 6e 78 2f 31  |.Server: nginx/1|
     00000020  2e 31 30 2e 32 0d 0a 44  61 74 65 3a 20 54 68 75  |.10.2..Date: Thu|
     00000030  2c 20 30 33 20 4e 6f 76  20 32 30 31 36 20 32 32  |, 03 Nov 2016 22|
     00000040  3a 35 38 3a 30 32 20 47  4d 54 0d 0a 43 6f 6e 74  |:58:02 GMT..Cont|
     00000050  65 6e 74 2d 54 79 70 65  3a 20 61 70 70 6c 69 63  |ent-Type: applic|
     00000060  61 74 69 6f 6e 2f 6a 73  6f 6e 0d 0a 43 6f 6e 74  |ation/json..Cont|
     00000070  65 6e 74 2d 4c 65 6e 67  74 68 3a 20 31 38 35 30  |ent-Length: 1850|
     00000080  0d 0a 43 6f 6e 6e 65 63  74 69 6f 6e 3a 20 63 6c  |..Connection: cl|
     00000090  6f 73 65 0d 0a 45 74 61  67 3a 20 33 2e 34 2e 30  |ose..Etag: 3.4.0|
     000000a0  2e 67 70 68 67 73 70 33  77 7a 69 6e 6e 78 70 78  |.gphgsp3wzinnxpx|
     000000b0  6f 63 6e 71 79 6f 74 70  33 33 72 2e 31 34 37 38  |ocnqyotp33r.1478|
     000000c0  32 30 34 36 36 35 32 38  32 2e 31 34 37 37 36 30  |204665282.147760|
     000000d0  32 34 35 39 32 36 34 2e  33 0d 0a 45 78 70 69 72  |2459264.3..Expir|
     000000e0  65 73 3a 20 30 0d 0a 58  2d 43 6c 75 73 74 65 72  |es: 0..X-Cluster|
     000000f0  2d 49 64 3a 20 0d 0a 58  2d 52 61 74 65 6c 69 6d  |-Id: ..X-Ratelim|
     00000100  69 74 2d 4c 69 6d 69 74  3a 20 31 30 0d 0a 58 2d  |it-Limit: 10..X-|
     00000110  52 61 74 65 6c 69 6d 69  74 2d 52 65 6d 61 69 6e  |Ratelimit-Remain|
     00000120  69 6e 67 3a 20 39 0d 0a  58 2d 52 61 74 65 6c 69  |ing: 9..X-Rateli|
     00000130  6d 69 74 2d 52 65 73 65  74 3a 20 31 0d 0a 58 2d  |mit-Reset: 1..X-|
     00000140  52 65 71 75 65 73 74 2d  49 64 3a 20 69 70 69 75  |Request-Id: ipiu|
     00000150  7a 63 6a 7a 66 37 38 79  38 6b 70 73 7a 6a 73 69  |zcjzf78y8kpszjsi|
     00000160  39 66 6f 69 7a 63 0d 0a  58 2d 56 65 72 73 69 6f  |9foizc..X-Versio|
     00000170  6e 2d 49 64 3a 20 33 2e  34 2e 30 2e 33 2e 34 2e  |n-Id: 3.4.0.3.4.|
     00000180  30 2e 63 61 37 36 34 65  64 39 62 65 33 32 36 35  |0.ca764ed9be3265|
     00000190  30 35 35 37 37 65 30 36  63 64 66 61 66 39 63 65  |05577e06cdfaf9ce|
     000001a0  62 37 0d 0a 0d 0a 7b 22  63 68 61 6e 6e 65 6c 73  |b7....{"channels|
     000001b0  22 3a 5b 7b 22 69 64 22  3a 22 67 70 68 67 73 70  |":[{"id":"gphgsp|
     000001c0  33 77 7a 69 6e 6e 78 70  78 6f 63 6e 71 79 6f 74  |3wzinnxpxocnqyot|
     000001d0  70 33 33 72 22 2c 22 63  72 65 61 74 65 5f 61 74  |p33r","create_at|
     000001e0  22 3a 31 34 37 38 31 34  36 32 38 30 38 32 39 2c  |":1478146280829,|
     000001f0  22 75 70 64 61 74 65 5f  61 74 22 3a 31 34 37 38  |"update_at":1478|
     00000200  31 34 36 32 38 30 38 32  39 2c 22 64 65 6c 65 74  |146280829,"delet|
     00000210  65 5f 61 74 22 3a 30 2c  22 74 65 61 6d 5f 69 64  |e_at":0,"team_id|
     00000220  22 3a 22 6f 78 6e 61 7a  79 78 31 6b 74 72 6e 7a  |":"oxnazyx1ktrnz|
     00000230  71 72 6a 38 7a 39 61 33  6f 73 36 6d 72 22 2c 22  |qrj8z9a3os6mr","|
<snip>

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

Need help to resolve this further please.

Hi @prateeksriv,

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 {
1 Like

This has been fixed in the sample code, but there appears to be another bug that will be fixed by this community PR once that gets merged.