Photon Releases Spectrum: An Open-Source TypeScript Framework that Deploys AI Agents Directly to iMessage, WhatsApp, and Telegram
Back to Tutorials
techTutorialbeginner

Photon Releases Spectrum: An Open-Source TypeScript Framework that Deploys AI Agents Directly to iMessage, WhatsApp, and Telegram

April 21, 20261 views5 min read

Learn to build and deploy AI chatbots using Photon's open-source Spectrum framework that works directly on iMessage, WhatsApp, and Telegram.

Introduction

In this tutorial, you'll learn how to build and deploy a simple AI chatbot using the open-source Spectrum framework from Photon. This framework allows you to deploy AI agents directly to popular messaging platforms like iMessage, WhatsApp, and Telegram. By the end of this tutorial, you'll have a working chatbot that can respond to messages on these platforms.

Why is this important? Traditional AI agents are often limited to developer dashboards or specialized apps. Spectrum solves this by making AI agents directly accessible through the messaging platforms people already use every day.

Prerequisites

Before you begin, you'll need:

  • A computer with internet access
  • Node.js installed (version 16 or higher)
  • A basic understanding of TypeScript
  • Access to a messaging platform (iMessage, WhatsApp, or Telegram)
  • A free account on a messaging platform's developer portal (for WhatsApp and Telegram)

Why these prerequisites? Node.js is needed to run the TypeScript code, and you'll need a messaging platform account to set up webhooks for receiving messages.

Step-by-Step Instructions

1. Setting Up Your Development Environment

First, create a new directory for your project and initialize it with npm:

mkdir spectrum-chatbot
 cd spectrum-chatbot
 npm init -y

This creates a new project directory and initializes it with a package.json file.

2. Installing Spectrum Framework

Install the Spectrum framework and its dependencies:

npm install @photon/spectrum
npm install typescript ts-node @types/node

The @photon/spectrum package contains the core framework, while TypeScript and related tools help with type checking and development.

3. Creating Your First AI Agent

Create a new file called agent.ts in your project directory:

import { SpectrumAgent } from '@photon/spectrum';

const agent = new SpectrumAgent({
  name: 'My Chatbot',
  description: 'A simple chatbot built with Spectrum',
  greeting: 'Hello! I am your AI assistant.',
});

// Define how your agent responds to messages
agent.on('message', (message) => {
  console.log(`Received message: ${message.text}`);
  
  // Simple response logic
  if (message.text.toLowerCase().includes('hello')) {
    return 'Hi there! How can I help you today?';
  } else if (message.text.toLowerCase().includes('help')) {
    return 'I can help you with basic questions. Try asking me about weather or time.';
  } else {
    return 'I received your message! I am still learning, but I will do my best to assist you.';
  }
});

// Start the agent
agent.start();

This code creates a basic AI agent with custom responses. The agent listens for messages and responds based on keywords in the message text.

4. Configuring Platform Support

For each messaging platform, you'll need to configure webhook endpoints. Create a config.ts file:

export const platformConfig = {
  imessage: {
    enabled: true,
    webhookUrl: 'https://your-server.com/imessage-webhook',
  },
  whatsapp: {
    enabled: true,
    webhookUrl: 'https://your-server.com/whatsapp-webhook',
    accessToken: 'your-whatsapp-access-token',
  },
  telegram: {
    enabled: true,
    webhookUrl: 'https://your-server.com/telegram-webhook',
    botToken: 'your-telegram-bot-token',
  },
};

Update the webhook URLs and tokens with your actual platform credentials. These endpoints will receive messages from the platforms.

5. Setting Up Webhooks

Create a simple Express server to handle incoming messages:

import express from 'express';
import { platformConfig } from './config';
import { SpectrumAgent } from '@photon/spectrum';

const app = express();
app.use(express.json());

const agent = new SpectrumAgent({
  name: 'My Chatbot',
  description: 'A simple chatbot built with Spectrum',
  greeting: 'Hello! I am your AI assistant.',
});

// Handle iMessage webhook
app.post('/imessage-webhook', (req, res) => {
  const message = req.body;
  agent.handleMessage(message);
  res.status(200).send('Message received');
});

// Handle WhatsApp webhook
app.post('/whatsapp-webhook', (req, res) => {
  const message = req.body;
  agent.handleMessage(message);
  res.status(200).send('Message received');
});

// Handle Telegram webhook
app.post('/telegram-webhook', (req, res) => {
  const message = req.body;
  agent.handleMessage(message);
  res.status(200).send('Message received');
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

This server listens for messages on three different endpoints and forwards them to your AI agent.

6. Running Your Chatbot

Compile and run your TypeScript code:

npx tsc
node dist/agent.js

Or, for development, you can use:

npx ts-node agent.ts

This starts your chatbot and makes it ready to receive messages.

7. Testing Your Chatbot

Test your chatbot by sending messages to your configured platforms:

  • For iMessage: Send a message to your computer's iMessage
  • For WhatsApp: Use the WhatsApp web interface or mobile app
  • For Telegram: Start a chat with your bot

When you send a message, your agent should respond according to the logic you defined in step 3.

Summary

In this tutorial, you've learned how to create a basic AI chatbot using the Spectrum framework from Photon. You've set up your development environment, created a simple AI agent with custom responses, configured webhook endpoints for messaging platforms, and tested your chatbot on iMessage, WhatsApp, and Telegram.

This framework solves the problem of AI agents being isolated in developer tools by deploying them directly to platforms people already use. The code you've written provides a foundation that you can expand upon with more sophisticated AI capabilities, additional platforms, and advanced features.

Remember to keep your webhook URLs updated and your platform tokens secure. As you develop more complex chatbots, consider adding features like natural language processing, database integration, and multi-platform support.

Source: MarkTechPost

Related Articles