簡體   English   中英

使用 nodejs 向微軟團隊頻道發送消息

[英]Send message to microsoft teams channel using nodejs

我能夠獲取訪問令牌,但不確定如何發送消息,因為它需要用戶並且我的應用程序是后端應用程序(nodejs 腳本)。 在圖形資源管理器上,它可以工作。

圖資源管理器上的代碼片段是:

const options = {
    authProvider, //I need this value
};

const client = Client.init(options);

const chatMessage = {body: {content: '@.@@@@.@'}};

await client.api('/teams/my-team-id/channels/my-channel-id/messages')
    .post(chatMessage);

如何在 nodejs 中獲取 authProvider? 我嘗試使用 MSALAuthenticationProviderOptions 但似乎存在問題(如他們的 github 存儲庫中所述),請按照以下步驟操作:https://www.npmjs.com/package/@microsoft/microsoft-graph-client

您需要在應用程序而不是用戶的上下文中運行它。 Microsoft Graph JavaScript 庫現在支持 Azure TokenCredential 用於獲取令牌。

const { Client } = require("@microsoft/microsoft-graph-client");
const { TokenCredentialAuthenticationProvider } = require("@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials");
const { ClientSecretCredential } = require("@azure/identity");
const { clientId, clientSecret, scopes, tenantId } = require("./secrets"); // Manage your secret better than this please.

require("isomorphic-fetch");

async function runExample() {
    const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
    const authProvider = new TokenCredentialAuthenticationProvider(credential, { scopes: [scopes] });
    const client = Client.initWithMiddleware({
        debugLogging: true,
        authProvider,
    });

    const chatMessage = {body: {content: '@.@@@@.@'}};
    const res = await client.api('/teams/my-team-id/channels/my-channel-id/messages')
                            .post(chatMessage);
    console.log(res);
}

runExample().catch((err) => {
    console.log("Encountered an error:\n\n", err);
});

該樣本來自:

https://github.com/microsoftgraph/msgraph-sdk-javascript/tree/dev/samples/tokenCredentialSamples/ClientCredentialFlow

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM