Facebook Messenger BOT-将图像发送给Messenger用户

时间:2020-06-04 20:25:03

标签: c# facebook-graph-api file-upload facebook-messenger-bot

我已经在 C#中开发了 Messenger BOT ,并且除向使用BOT的用户发送本地文件(图像或PDF)外,其他所有功能都正常运行。

我正在尝试通过以下功能发送文件,但始终会收到错误消息“ StatusCode:400,ReasonPhrase:'Bad Request'”。

        private async Task UploadFileAndSendToFBUser( )
        {
        string senderUserId = "1234567890";
        string filepath = @"C:\PROJECTS\Messenger\MessengerBot-WebAPI-master\MessengerBot-WebAPI-master\MessengerBot\SfondoAQL1.png";

        using (var httpClient = new HttpClient())
        using (var formDataContent = new MultipartFormDataContent())
        { 
            var fileStream = File.OpenRead(filepath);
            using (var streamContent = new StreamContent(fileStream))
            { 
                var imageContent = new ByteArrayContent(streamContent.ReadAsByteArrayAsync().Result);
                imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");

                formDataContent.Add(imageContent, "image", "SfondoAQL1.png");
                formDataContent.Add(new StringContent($"{{\"id\":\"{long.Parse(senderUserId)}\"}}"), "recipient");
                formDataContent.Add(new StringContent($"{{\"attachment\":{{\"type\":\"{"image"}\", \"payload\":{{\"is_reusable\"=true}}}}"), "message");

                string url = "https://graph.facebook.com/v2.6/me/messages?access_token=ssssssssssssssssssssssssssssssssssss";
                var response = httpClient.PostAsync(url, formDataContent).Result; 
            }
         }
      }

有人可以帮助我吗? 谢谢。

我想在C#(本地磁盘中的PDF文件)中做类似的事情

https://developers.facebook.com/docs/messenger-platform/send-messages#file

0 个答案:

没有答案