botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

ChannelData form Slack is not a valid format

Open gcsonka opened this issue 10 years ago • 7 comments

When Slack sends a message to my bot I receive some information in the ChannelData property that I like to access. However the Channeldata comes in this format (value copied form the Watch window):

{{ "SlackMessage": { "type": "message", "channel": "D0ZFJ5ECR", "user": "U0Z1D812M", "text": "news", "ts": "1475639797.000055", "team": "T0YDZTCBD" }, "ApiToken": "some token" }}

Notice the "{{" section.

Based on this: https://docs.botframework.com/en-us/csharp/builder/sdkreference/channels.html

I believe I should be getting something like

{ "Payload":{ "SlackMessage": { "type": "message", "channel": "D0ZFJ5ECR", "user": "U0Z1D812M", "text": "news", "ts": "1475639797.000055", "team": "T0YDZTCBD" }, "ApiToken": "some token" }}

But I seem to be missing the "Payload:" element.

Is there a work around this? How can I access the Channeldata fields?

Thank you

gcsonka avatar Oct 05 '16 04:10 gcsonka

The "Payload" section you are referring to is only sent in response to a button click event in Slack. A normal message from Slack should contain ChannelData with two properties; SlackMessage and ApiToken. SlackMessage is the raw message that was sent to the channel by Slack and ApiToken is your bot's Slack Api token that you can use to make direct Slack api calls if needed.

I'm not sure why you are seeing double brackets. Please add your bot Id and I'll dig into our logs and see if I can figure out what's going on.

craigjensen avatar Oct 07 '16 00:10 craigjensen

Thanks. My bot app id is 122dd7ca-2596-4d72-b679-c696e212987a

If I could get rid of the double brackets somehow that would help.

gcsonka avatar Oct 07 '16 01:10 gcsonka

I'm not seeing any malformed messages in the logs. Are you using the C# or Js sdk?

craigjensen avatar Oct 07 '16 15:10 craigjensen

Hi Craig - I am using c# version 3.2.1. Thanks for looking into this.

gcsonka avatar Oct 08 '16 19:10 gcsonka

I confirmed that the c# sdk doesn't alter the channeldata property. What do you get when you serialize the incoming activity message via JsonConvert.SerializeObject(message)?

craigjensen avatar Oct 10 '16 18:10 craigjensen

I solved it. The problem was that I have tried to serialize the IMessageActivity object directly like this: ActivityJson = new JavaScriptSerializer().Serialize(Activity);

When done like this the above Activity.ChannelData property serializes to this result:

[[[[[]],[[]],[[]],[[]],[[]],[[]]]],[[]]]

But if I use this code to serialize the ChannelData property separately than it works fine:

JObject cdata = Activity.ChannelData as JObject; if (cdata != null ) { log.ChannelDataJson = cdata.ToObject().ToString(); }

The result of this is something like this (which is what I needed): { "SlackMessage": { "type": "message", "channel": "D0ZFJ5ECR", "user": "U0Z1D812M", "text": "news from <#C0YFSM50S|general>", "ts": "1475997246.000095", "team": "T0YDZTCBD" }, "ApiToken": "some token"}

Thank you for your help.

gcsonka avatar Oct 11 '16 00:10 gcsonka

Glad to hear. I'll go ahead and close this issue for now and you can reopen if you have additional questions.

craigjensen avatar Oct 11 '16 00:10 craigjensen