问题
I have been trying to post the images along with the message in Microsoft Teams via MS Graph API using C# but unable to do so.
Below is the code I have tried:
string userName = ConfigurationManager.AppSettings["UserName"];
string password = ConfigurationManager.AppSettings["Password"];
System.Security.SecureString passWordSecureString = new System.Security.SecureString();
foreach (char c in password.ToCharArray()) passWordSecureString.AppendChar(c);
var clientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
var tenantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
string[] scopes = { "ChannelMessage.Send", "Group.ReadWrite.All", "User.Read" };
IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenantId)
.Build();
//creating the graph user context.
UsernamePasswordProvider authProvider = new UsernamePasswordProvider(publicClientApplication, scopes);
var chatMessage = new ChatMessage
{
Subject = null,
Body = new ItemBody
{
ContentType = BodyType.Html,
Content = "Hello world </br><div><div>\n<div><span><img height=\"297\" src=\"../hostedContents/1/$value\" width=\"297\" style=\"vertical-align:bottom; width:297px; height:297px\"></span>\n\n</div>\n\n\n</div>\n</div>"
},
};
ChatMessageHostedContent chatMessageHostedContent = new ChatMessageHostedContent
{
ContentBytes = Encoding.ASCII.GetBytes("iVBORw0KGgoAAAANSUhEUgAAASkA..."),
ContentType = "image/png",
AdditionalData = new Dictionary<string, object>()
{
{"@microsoft.graph.temporaryId", "1"}
}
};
IChatMessageHostedContentsCollectionPage chatMessageHostedContentsCollectionPage = new ChatMessageHostedContentsCollectionPage();
chatMessageHostedContentsCollectionPage.Add(chatMessageHostedContent);
if (chatMessageHostedContentsCollectionPage.Count > 0)
chatMessage.HostedContents = chatMessageHostedContentsCollectionPage;
ChatMessage sentMessage = await graphClient.Teams["{id}"].Channels["{id}"].Messages
.Request()
.AddAsync(chatMessage);
The image is getting uploaded but it's not showing anything i.e. it's broken/corrupted as shown below:
I have even tried the below code to add an image to the sent message but its throwing an "unknown error" exception.
await graphClient.Teams["{id}"].Channels["{id}"].Messages[sentMessage.Id].HostedContents
.Request()
.AddAsync(chatMessageHostedContent);
I am able to post the same image along with text successfully using the Post HTTP method in MS Graph Explorer with below URL & payload.
https://graph.microsoft.com/beta/teams/{id}/channels/{id}/messages
{"body":{"contentType":"html","content":"Hello world </br><div><div>\n<div><span><img height=\"297\" src=\"../hostedContents/1/$value\" width=\"297\" style=\"vertical-align:bottom; max-width: 100%; height:auto\"></span>\n\n</div>\n\n\n</div>\n</div>"},"hostedContents":[{"@microsoft.graph.temporaryId":"1","contentBytes":"iVBORw0KGgoAAAANSUhEUgAAASkAAAEpCA...","contentType":"image/png"}]}
Can anybody suggest what is wrong in the code?
来源:https://stackoverflow.com/questions/64028614/unable-to-post-inline-images-along-with-the-message-in-microsoft-teams-via-micro