question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Slack Adapter throws JsonReaderException: Unexpected Token: StartObject when processing "reaction_added" event

See original GitHub issue

Github 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:

  1. 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
  2. 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);

                }
            }
        }
  1. Startup the bot and direct-message the bot on its attached Slack channel to get a message echoed back.
  2. 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:closed
  • Created 3 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
garyprettycommented, Sep 21, 2020

@tdurnford this should be on plan to fix in 4.11. @mrivera-ms this bug should be addressed as part of the next release.

1reaction
benbrowncommented, Sep 8, 2020

Thank you for confirming that! I’ll get back to you ASAP when we learn more.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parsing with Json.NET: "Unexpected token: StartObject"
I am using the Newtonsoft.Json.NET dll. Error reading string. Unexpected token: StartObject. Path '[0]', line 1, position 2.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found