Jump to content

Sending multipart/form-data requests with the Emby SDK


Recommended Posts

darkside40
Posted

Okay so i am trying to update my Telegram Plugin to also send images on updates. Which is quite more complicated that i thought. althought i have tried to get helped by Claude etc. most of the LLM's recommendation were useless. Really surprising. On the other hand i a a really bad c# coder.

I could try to go the easy way, letting the user expose their Emby Server to the internet so Telegram could grab the image directly from there. Thats the way the the Slack and the Discord Plugins do it, althought it is a long time broken in Emby 4.8.11. I dont think thats a good idea. It is too complicated for the user, and (at least in 4.8) this API Endpoint does not require a token etc. so everybody could simply enumerate throught the posters you have on your server. Might not be dangerous but ugly.

According to Telegrams Bot API you can use a multipart/form-data request to send them the image: https://core.telegram.org/bots/api#sendphoto

But i am just not smart enough to figure out how.

I already got that code snippet:

                // if the raw bytes are needed
                 var originalImageInfo = await image.GetImageInfo(cancellationToken).ConfigureAwait(false);
                 var bytes = await _fileSystem.ReadAllBytesAsync(originalImageInfo.Path, cancellationToken).ConfigureAwait(false);
                
                 var url = $"https://api.telegram.org/bot-insertTokeHere/sendPhoto";
                
                MultipartFormDataContent content = new MultipartFormDataContent();
                content.Add(new StringContent(chatID), "chat_id");
                ByteArrayContent imageContent = new ByteArrayContent(bytes);
                imageContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/jpeg");
                content.Add(imageContent, "photo");

                var httpRequestOptions = new HttpRequestOptions
                {
                    Url = "https://api.telegram.org/bot-insertTokeHere/sendPhoto",
                    RequestContent = content
                };

                using (await _httpClient.Post(httpRequestOptions).ConfigureAwait(false))
                {

                }

Which fails with a CS0029.

So could some experienced Developer give me a hand how to do that?

darkside40
Posted

Nevermind, found a way

Posted

Hi, what was the solution?

darkside40
Posted

Take a look at the notifier.cs of my Telegram Notification Plugin

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...