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.1 KB
Raw Blame
import {
  DedupeStrategy,
  httpClient,
  HttpMethod,
  Polling,
  pollingHelper,
} from '@activepieces/pieces-common';
import {
  AppConnectionValueForAuthProperty,
  createTrigger,
  Property,
  StaticPropsValue,
  TriggerStrategy,
} from '@activepieces/pieces-framework';
import dayjs from 'dayjs';
import { twitchAuth } from '../auth';

const props = {
  streamers: Property.Array({
    displayName: 'Streamer Name(s)/URL(s)',
    description:
      'Add the usernames or URLs of the streamers you want to monitor (e.g., loltyler1 or https://twitch.tv/faker).',
    required: true,
  }),
};
const polling: Polling<
  AppConnectionValueForAuthProperty<typeof twitchAuth>,
  StaticPropsValue<typeof props>
> = {
  strategy: DedupeStrategy.TIMEBASED,
  items: async ({ auth, propsValue }) => {
    const { streamers } = propsValue;

    const logins = (streamers as string[]).map((s) =>
      s.replace(/.*\//, '').trim().toLowerCase()
    );

    const queryString = logins
      .map((login) => `user_login=${encodeURIComponent(login)}`)
      .join('&');

    const response = await httpClient.sendRequest({
      method: HttpMethod.GET,
      url: `https://api.twitch.tv/helix/streams?${queryString}`,
      headers: {
        'Client-ID': auth.client_id,
        Authorization: `Bearer ${auth.access_token}`,
      },
    });

    const streams = response.body.data;

    return streams.map((s: any) => ({
      epochMs: dayjs(s.started_at).valueOf(),
      data: s,
    }));
  },
};

export const streamStarted = createTrigger({
  auth: twitchAuth,
  name: 'stream_started',
  classification: 'READ',
  displayName: 'Streamer(s) Go Live',
  description: 'Triggers when a stream from a list of streamers has started',
  aiMetadata: {
    description:
      'Fires when any of the monitored Twitch streamers goes live (transitions from offline to broadcasting). Polls the Twitch Helix streams API for the given user logins and emits one event per newly started stream. Each event represents an active live stream and includes the streamer (user_id, user_login, user_name), the game/category (game_id, game_name), stream title, current viewer_count, language, started_at timestamp, thumbnail_url, tags, and is_mature flag.',
  },
  props: props,
  sampleData: {
    id: '316465520862',
    user_id: '230609655',
    user_login: 'carnyjared',
    user_name: 'CarnyJared',
    game_id: '125264560',
    game_name: 'Clone Hero',
    type: 'live',
    title: '🚨CLONE HERO LEADERBOARD PTB🚨',
    viewer_count: 163,
    started_at: '2025-12-24T04:58:37Z',
    language: 'en',
    thumbnail_url:
      'https://static-cdn.jtvnw.net/previews-ttv/live_user_carnyjared-{width}x{height}.jpg',
    tag_ids: [],
    tags: ['English'],
    is_mature: false,
  },
  type: TriggerStrategy.POLLING,

  async test(context) {
    return await pollingHelper.test(polling, context);
  },
  async onEnable(context) {
    await pollingHelper.onEnable(polling, context);
  },

  async onDisable(context) {
    await pollingHelper.onDisable(polling, context);
  },

  async run(context) {
    return await pollingHelper.poll(polling, context);
  },
});