Microsoft Teams Bot Without an SDK
medium20 minteams-bot.ts
You want an agent in Microsoft Teams that answers personal chats and @mentions in channels, asks before risky tools run, and does not pull in botbuilder.
What it shows
- Run a
GatewaywithteamsChannel(): Bot Framework posts activities to/api/messages, answers go to the Bot Connector - Every request is checked against its signed JWT: RS256, issuer, App ID audience, expiry, service URL and the
msteamsendorsement - Each personal chat, group chat and channel thread is a conversation of its own, and the mention of the bot is stripped
- Answers stream into a message it edits, spaced under the 7 messages a second Teams allows per chat
- Approval prompts get Approve and Deny buttons as an Adaptive Card with
Action.Execute
The code
teams-bot.ts
import { Agent, Cogitator } from '@cogitator-ai/core';
import { Gateway, teamsChannel } from '@cogitator-ai/channels';
const appId = process.env.TEAMS_APP_ID;
const appPassword = process.env.TEAMS_APP_PASSWORD;
const tenantId = process.env.TEAMS_TENANT_ID;
const apiKey = process.env.GOOGLE_API_KEY;
if (!appId || !appPassword || !tenantId || !apiKey) {
throw new Error('Set TEAMS_APP_ID, TEAMS_APP_PASSWORD, TEAMS_TENANT_ID and GOOGLE_API_KEY');
}
const teams = teamsChannel({ appId, appPassword, tenantId, webhook: { port: 3978 } });
const agent = new Agent({
name: 'teams-assistant',
model: 'google/gemini-3.5-flash-lite',
instructions: 'You help a team in Microsoft Teams. Be brief and use Markdown.',
});
const cogitator = new Cogitator({
llm: { providers: { google: { apiKey } } },
memory: { adapter: 'memory' },
});
const memory = await cogitator.getMemory();
const gateway = new Gateway({
agent,
cogitator,
channels: [teams],
...(memory && { memory }),
stream: { flushInterval: 600, minChunkSize: 40 },
});
await gateway.start();
console.log('Teams messaging endpoint at http://localhost:3978/api/messages');Run it
install · bash
pnpm add @cogitator-ai/core @cogitator-ai/channelsEnvironment
TEAMS_APP_IDTEAMS_APP_PASSWORDTEAMS_TENANT_IDGOOGLE_API_KEYrun · bash
npx tsx teams-bot.tsOr run the full example from a clone of the repository (keys in .env):
repository · bash
npx tsx examples/channels/08-teams-bot.ts