Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SendToConversationAsync() returns BadSyntax (No valid activities found) intermittently #5061

Closed
ktundwal opened this issue Sep 18, 2018 · 2 comments

Comments

@ktundwal
Copy link

commented Sep 18, 2018

Hello, I am developing a bot where I have 2 types of users: agent and customer. customer talks to bot and when bot cant complete the request it uses agent to answer. I used ProactiveMsgTo1to1Dialog.cs example to send the message to agent on customer's behalf and vice-a-versa. I have saved conversation-id in my DB (agentConversationId and customerConversationId) for this.

Bot Info

Issue Description

First time customer sends the message to bot, bot is able to use SendToConversationAsync() and message appears in agent's channel. Agent replies and bot uses same technique to send message to customer. However any message afterwards (from customer -> bot -> agent) fails with SendToConversationAsync() returning BadSyntax (No valid activities found).

Code Example

        public static async Task<ResourceResponse> SendMessageToAgent(IMessageActivity activity,
            string messageToSend,
            string agentConversationId)
        {
            try
            {
                var botAccount = activity.Recipient;

                using (ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl),
                    System.Configuration.ConfigurationManager.AppSettings["MicrosoftAppId"],
                    System.Configuration.ConfigurationManager.AppSettings["MicrosoftAppPassword"]))
                {
                    IMessageActivity message = Activity.CreateMessageActivity();
                    message.From = botAccount;
                    message.Conversation = new ConversationAccount(id: agentConversationId);
                    message.Text = $"[{activity.From.Name}]: {messageToSend}";
                    message.ServiceUrl = activity.ServiceUrl;

                    ResourceResponse response = await connector.Conversations.SendToConversationAsync((Activity)message);

                    return response;
                }
            }
            catch (Exception e)
            {
                Trace.TraceError($"Error sending message to agent. Exception={e.Message}");
                throw;
            }
        }

Reproduction Steps

Described in issue

Expected Behavior

Message should be sent

Actual Results

request json

{
    "type": "message",
    "serviceUrl": "https://smba.trafficmanager.net/amer/",
    "from": {
        "id": "28:801bf31e-1421-48aa-bafc-45d2046503ad",
        "name": "EIBot  dev "
    },
    "conversation": {
        "id": "19:c20b196747424d8db51f6c00a8a*****@thread.skype;messageid=1537240455824"
    },
    "text": "some message here",
    "attachments": [],
    "entities": []
}

response json

{
    "error": {
        "code": "BadSyntax",
        "message": "No valid activities found"
    }
}

@ktundwal ktundwal changed the title SendToConversationAsync returns BadSyntax (No valid activities found) intermittently SendToConversationAsync() returns BadSyntax (No valid activities found) intermittently Sep 18, 2018

@siduppal

This comment has been minimized.

Copy link

commented Sep 18, 2018

Issue was due to activity text containing a string of the form "[bla]: foo" and textFormat defaulting to markdown. This should work if you set textFormat = "plain" or escape the text string if you need to keep textFormat = markdown for some reason.

If the other side is Teams, you will receive HTML or plain text messages in your bot, so if the scenario is to replay the message back into Teams, then you should set textFormat = xml.

@ktundwal

This comment has been minimized.

Copy link
Author

commented Sep 18, 2018

@siduppal suggestion worked. Here is the full code that works.

        public static async Task<ResourceResponse> SendMessageToAgent(IMessageActivity activity,
            string messageToSend,
            string agentConversationId)
        {
            try
            {
                var botAccount = activity.Recipient;

                using (ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl),
                    System.Configuration.ConfigurationManager.AppSettings["MicrosoftAppId"],
                    System.Configuration.ConfigurationManager.AppSettings["MicrosoftAppPassword"]))
                {
                    IMessageActivity message = Activity.CreateMessageActivity();
                    message.From = botAccount;
                    message.ReplyToId = agentConversationId;
                    message.Conversation = new ConversationAccount
                    {
                        Id = agentConversationId,
                        IsGroup = true,
                    };
                    message.Text = $"[{activity.From.Name}]: {messageToSend}";
                    message.TextFormat = "plain";
                    message.ServiceUrl = activity.ServiceUrl;
                    message.ChannelData = new Dictionary<string, object>
                    {
                        ["teamsChannelId"] = "19:c20b196747424d8db51f6c00a8a****@thread.skype",
                        ["notification"] = new Dictionary<string, object> { { "alert", true } }
                    };

                    ResourceResponse response = await connector.Conversations.SendToConversationAsync((Activity)message);
                    Trace.TraceInformation($"[SUCCESS]: SendMessageToAgent. Message={messageToSend}. " +
                                           $"response id ={response.Id} agentConversationId={agentConversationId} ");

                    return response;
                }
            }
            catch (Exception e)
            {
                Trace.TraceError($"[FAIL]: SendMessageToAgent. Error sending message to agent. Exception={e.Message}");
                throw;
            }
        }

@ktundwal ktundwal closed this Sep 18, 2018

@jwiley84 jwiley84 added the .NET SDK label Sep 27, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.