Home > OS >  Sending message to Team chat using c# application system
Sending message to Team chat using c# application system

Time:09-27

Currently I'm using the way of Microsoft Graph API beta you can send a message in a chat to write the application but I face these errors The type or namespace ChatMessage could not be found, The name authProvider does not exist in the current context, The await operator can only be used within an async modifier and changing its return type to tásk and Microsoft.GraphGraphServiceClient does not contain a definition for Chat and no extension method Chats accepting a first argument of type Microsoft.Graph.Graph.ServiceClient could be found. Can anyone help me to solve it.

Thanks a lot .

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Graph;

namespace w
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        
            GraphServiceClient graphClient = new GraphServiceClient(**authProvider**);

            var chatMessage = new **ChatMessage**
            {
                Body = new ItemBody
                {
                    Content = "Hello world"
                }
            };

            *await* graphClient.**Chats**["19:056680e9-868a-49a2-bf96-2ba2d2c39b5d_76ff09a8-7650-426d-843b-3d1104128f25@unq.gbl.spaces"].Messages
      .Request()
      .AddAsync(chatMessage);
        }
    }
}

}

CodePudding user response:

According the enter image description here

About the authProvider, that's the object contianing the authorization information. await* graphClient.**Chats**["19..... is calling the ms graph api, and we need the access token in the request header to call the api right? But now since you are using the graph SDK, so you only need to prepare the authProvider.

AuthProvider is something contained in Azure AD, and Azure AD offers you 2 kinds of flows, one for users sign in first to get the authorization, so you have to provide a sign in page in this scenarion, for example Authorization code provider. Another one for applications so there's no users sign in, you have to create an azure ad application then get a application client secret to valid the aad application in your own code, this is known as Client credentials provider. But Client credentials provider is not supported for this api.

So you may follow this section to create Auth code provider.

  • Related