I work at a company that asked me for an API that sends messages in a private chat in Teams
After my studies, I found the Microsoft Graph API and found out how to do it, in this link I can do it In this link I create the chat and send a message in the chat I created, from the id given to me when creating the chat
But when I do that, on my system it doesn't work
How I'm doing:
1- Create the Application
Here is my app permissions on azure
2 - I create a chat, and get the id that this call returns
This is the code i make for do this, i take the id after this
var user_email = IT.DsEmail;
var chat = new Chat
{
ChatType = ChatType.OneOnOne,
Members = new ChatMembersCollectionPage()
{
new AadUserConversationMember
{
Roles = new List<String>()
{
"owner"
},
AdditionalData = new Dictionary<string, object>()
{
{"user@odata.bind", "https://graph.microsoft.com/v1.0/users('notificacoes@vaxxinova.com.br')"}
}
},
new AadUserConversationMember
{
Roles = new List<String>()
{
"owner"
},
AdditionalData = new Dictionary<string, object>()
{
{"user@odata.bind", "https://graph.microsoft.com/v1.0/users('" + user_email + "')"}
}
}
}
};
var user = await graphServiceClient.Chats.Request().AddAsync(chat);
This works! I get the id with this call, and this id I would use to send the message
3 - Send a mesage in private chat
Now dont work! This is my code
var chatMessage = new ChatMessage
{
Body = new ItemBody
{
Content = "Hello world"
}
};
await graphServiceClient.Chats["19:35fa8078-de1a-4ec1-bb8f-85f1d4e14fa9_8848a24c-c679-415c-ac14-7f9dde86d6ec@unq.gbl.spaces"].Messages
.Request()
.AddAsync(chatMessage);
When i do that, i receive this error
Unauthorized\r\nMessage: Message POST is allowed in application-only context only for import purposes. Refer to https://docs.microsoft.com/microsoftteams/platform/graph-api/import-messages/import-external-messages-to-teams for more details.
Can someone help me? Because in that link to test I can create the chat and send the message, but in the system I'm creating I can't? And why in my system I can create the chat, but I can't send the message?
I create the aplication, i create the chat, but i receive a error when i tried send a mensagem