Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

3.4 KB
Raw Blame
import { PieceAuth, Property } from '@activepieces/pieces-framework';
import { createGraphClient, getAppOnlyToken, GRAPH_DEFAULT_SCOPE, withGraphRetry } from './common/graph';

const authDesc = `
Register a **single-tenant** Azure Bot in your own Microsoft tenant, then paste its credentials below. Microsoft deprecated multi-tenant bots, so the bot must live in the **same tenant** you use Microsoft Teams from. Sign in to the [Azure Portal](https://portal.azure.com) with an admin of that tenant.

📖 **[Full step-by-step guide (bot setup + packaging the Teams app)](https://github.com/activepieces/activepieces/blob/main/packages/pieces/community/microsoft-teams-bot/teams-app/README.md)**: the steps below are the short version.

**1. Create the Azure Bot**
- Create an **Azure Bot** resource → **Pricing tier: Free (F0)** → **Type of App: Single Tenant** → **Create new Microsoft App ID**. This creates the App Registration.

**2. Enable the Teams channel**
- On the Bot resource → **Channels** → add **Microsoft Teams** and Apply. (Without this, installing the app fails.)

**3. Set the messaging endpoint**
On the Bot resource → **Configuration** → **Messaging endpoint**, paste:

\`\`\`text
{{frontendUrl}}/api/v1/app-events/microsoft-teams-bot
\`\`\`

Azure requires a messaging endpoint. The app **must** be installed in the team for sending to work.

**4. Create a secret**
- App Registration → **Certificates & secrets** → **New client secret** → copy the **Value** (not the Secret ID).

**5. Grant Graph permissions (for the Team & Channel dropdowns)**
- App Registration → **API permissions** → **Add → Microsoft Graph → Application permissions** → add \`Team.ReadBasic.All\` and \`Channel.ReadBasic.All\` → **Grant admin consent** (both must show green).

**6. Install the bot in your team**
- Package your Teams app (\`manifest.json\` \`botId\` = this App ID) and add it to the team/channel you want to post to. Sending fails until the bot is a member of the team.

Then fill in:
- **Bot App ID**: the Application (client) ID
- **Bot App Secret**: the client secret **Value**
- **Tenant ID**: your Directory (tenant) ID`;

export const microsoftTeamsBotAuth = PieceAuth.CustomAuth({
  description: authDesc,
  required: true,
  props: {
    appId: Property.ShortText({
      displayName: 'Bot App ID',
      required: true,
    }),
    appSecret: PieceAuth.SecretText({
      displayName: 'Bot App Secret',
      required: true,
    }),
    tenantId: Property.ShortText({
      displayName: 'Tenant ID',
      description:
        'Your Directory (tenant) ID. Must be the same tenant you use Microsoft Teams from. The bot is single-tenant and cannot post into another tenant.',
      required: true,
    }),
  },
  validate: async ({ auth }) => {
    try {
      const token = await getAppOnlyToken({
        tenantId: auth.tenantId,
        appId: auth.appId,
        appSecret: auth.appSecret,
        scope: GRAPH_DEFAULT_SCOPE,
      });
      const client = createGraphClient(token);
      await withGraphRetry(() => client.api('/teams').top(1).get());
      return { valid: true };
    } catch (error) {
      const detail =
        (error as { response?: { body?: unknown } })?.response?.body ??
        (error as { body?: unknown })?.body ??
        (error as Error)?.message ??
        error;
      return {
        valid: false,
        error: `Auth failed: ${JSON.stringify(detail)}`,
      };
    }
  },
});