Slack Adapter throws JsonReaderException: Unexpected Token: StartObject when processing "reaction_added" event
See original GitHub issueGithub issues should be used for bugs and feature requests. Use Stack Overflow for general “how-to” questions.
Version
using .NET CLI 3.1.302 Bot Framework SDK 4.10.2 The bot is run with Slack using ngrok 2.3.35
Describe the bug
My goal was to update the EchoBot base template have the bot react to a ‘reaction_added’ Slack event (adding a reaction emoji to a bot’s message) by responding with a message (something like “Thanks for confirming”…). However the SlackAdapter fails to parse the JSON request I get from Slack on a ‘reaction_added’ event, creating a 500 Internal Server Error on my ngrok server and this stack trace in the .NET application:
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request.
Newtonsoft.Json.JsonReaderException: Error reading string. Unexpected token: StartObject. Path 'event.item', line 1, position 149.
at Newtonsoft.Json.JsonReader.ReadAsString()
at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member,
String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member,
String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
at Newtonsoft.Json.Linq.JToken.ToObject(Type objectType, JsonSerializer jsonSerializer)
at Newtonsoft.Json.Linq.JToken.ToObject(Type objectType)
at Newtonsoft.Json.Linq.JToken.ToObject[T]()
at Microsoft.Bot.Builder.Adapters.Slack.SlackAdapter.ProcessAsync(HttpRequest request, HttpResponse response, IBot bot, CancellationToken cancellationToken)
I think the problem is that the JSON deserialization happening within the SlackAdapter cannot handle nested objects; the JSON body received from a ‘reaction_added’ event looks like this: ( from the Events API page: https://api.slack.com/events/reaction_added )
{
"type": "reaction_added",
"user": "U024BE7LH",
"reaction": "thumbsup",
"item_user": "U0G9QF9C6",
"item": {
"type": "message",
"channel": "C0G9QF9GZ",
"ts": "1360782400.498405"
},
"event_ts": "1360782804.083113"
}
A ‘message’ event, which the bot and Slack Adapter handles correctly, looks like this - no nested object:
{
"type": "message",
"channel": "C2147483705",
"user": "U2147483697",
"text": "Hello world",
"ts": "1355517523.000005"
}
To Reproduce
Steps to reproduce the behavior:
- Set up the base EchoBot template with Slack Adapter as found in this sample: https://github.com/microsoft/BotBuilder-Samples/tree/main/samples/csharp_dotnetcore/60.slack-adapter
- Add the following code snippet to
EchoBot.cs
to handle ‘reaction_added’ events:
protected override async Task OnEventActivityAsync(ITurnContext<IEventActivity> turnContext, CancellationToken cancellationToken)
{
if(turnContext.Activity.Value is EventType slackEvent) {
// FIXME: Bot Framework SDK can't handle nested objects?
if(slackEvent.Type == "reaction_added") {
await turnContext.SendActivityAsync(MessageFactory.Text("Thank you for reacting!"), cancellationToken);
}
}
}
- Startup the bot and direct-message the bot on its attached Slack channel to get a message echoed back.
- React to that message with any reaction emoji.
Expected behavior
Bot should correctly parse the JSON request and respond with a message to the ‘reaction_added’ event.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (6 by maintainers)
Top GitHub Comments
@tdurnford this should be on plan to fix in 4.11. @mrivera-ms this bug should be addressed as part of the next release.
Thank you for confirming that! I’ll get back to you ASAP when we learn more.