1

I am trying to route an attachment sent to bot from one user to another. It works fine while I'm trying to send something from emulator to emulator/telegram/skype, but doesn't work at all when I try to send it from skype/telegram (skype says nothing, telegram argues: "POST to mybot failed: POST to the bot's endpoint failed with HTTP status 500"). Where should I seek to find out the problem?

Here is my code:

if (mes.Attachments != null && mes.Attachments.Any())
            {
                var list = new List<Attachment>();
                foreach (var attachment in mes.Attachments)
                {
                    using (HttpClient httpClient = new HttpClient())
                    {
                        // Skype attachment URLs are secured by a JwtToken, so we need to pass the token from our bot.
                        if (mes.ChannelId.Equals("skype", StringComparison.InvariantCultureIgnoreCase) &&
                            new Uri(attachment.ContentUrl).Host.EndsWith("skype.com"))
                        {
                            var mstoken = await new MicrosoftAppCredentials().GetTokenAsync();
                            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",
                            mstoken);
                        }

                        var responseMessage = await httpClient.GetAsync(attachment.ContentUrl);

                        var ms = await ConvertContentToByteArray(responseMessage);    

                        var sendAttachment = new Attachment()
                        {
                            ContentType = attachment.ContentType,
                            Name = attachment.Name,
                            ContentUrl = String.Format("data:image/png;base64,{1}", attachment.ContentType,
                            Convert.ToBase64String(ms))
                        };

                        list.Add(sendAttachment);
                    }
                }
                message.Attachments = list;
                message.AttachmentLayout = mes.AttachmentLayout;
            }

await connector.Conversations.SendToConversationAsync((Activity)message);

"mes" is incoming message, "message - outcoming.

Thank you in advance for your answers!

UPDATE 1: I managed to solve my problem with sending/receiving files (see my answer below). Unfortunately a new problem of a file size limit appeared (see Useful post).

Some links that were useful for me: Send Attachment Sample, Receive Attachment Sample, Useful post, Some other receiving example

Community
  • 1
  • 1
Sovan
  • 168
  • 1
  • 3
  • 12
  • 1
    The error message "POST to the bot's endpoint failed with HTTP status 500" implies that your bot is throwing a 500. Try attaching a debugger and see if you can catch it? – Lars Nov 23 '16 at 18:31
  • Thanks for your answer! I didn't realize that it is possible to debug an application directly in the cloud (not in the emulator). Here is link how to do that [Remote debugging web apps](https://learn.microsoft.com/en-us/azure/app-service-web/web-sites-dotnet-troubleshoot-visual-studio). I also had to apply a patch from [this answer](http://stackoverflow.com/questions/17659473/visual-studio-remote-debugger-invalid-access-to-memory-location-feature) to make it work. – Sovan Nov 24 '16 at 12:42

1 Answers1

1

The above code is correct and works fine (if the size of an attachment isn't too large). My problem (and silly fault) was in using mes.Text in some other place without null check. Attachments in Skype/Telegram have mes.Text == null.

Sovan
  • 168
  • 1
  • 3
  • 12