@tooly/twilio
v0.0.7
Published
Twilio API tools for OpenAI, Anthropic, and AI SDK
Maintainers
Readme
@tooly/twilio
Twilio API tools for AI applications, compatible with OpenAI function calling, Anthropic tool use, and AI SDK.
Installation
npm install @tooly/twilio
# or
yarn add @tooly/twilio
# or
pnpm add @tooly/twilioSetup
Get your Twilio Account SID and Auth Token from your Twilio Console.
Usage
Basic Usage
import { TwilioTools } from '@tooly/twilio'
const twilio = new TwilioTools('your-account-sid', 'your-auth-token')
// Get available tools for function calling
const tools = twilio.getTools()
// Execute a function
const result = await twilio.executeFunction('sendSms', {
to: '+1234567890',
body: 'Hello from Twilio!',
from: '+0987654321',
})AI SDK
import { createAITools } from '@tooly/twilio'
const tools = createAITools('your-account-sid', 'your-auth-token')
// Use with generateText
import { generateText } from 'ai'
const result = await generateText({
model: openai('gpt-4-turbo'),
messages: [{ role: 'user', content: 'Send an SMS to +1234567890 saying hello' }],
tools,
})OpenAI Function Calling
import { createOpenAIFunctions } from '@tooly/twilio'
const { tools, executeFunction } = createOpenAIFunctions('your-account-sid', 'your-auth-token')
// Use with OpenAI client
const completion = await openai.chat.completions.create({
model: 'gpt-4-turbo',
messages: [{ role: 'user', content: 'Make a call to +1234567890' }],
tools,
})
// Execute function calls
for (const toolCall of completion.choices[0].message.tool_calls || []) {
const result = await executeFunction(toolCall.function.name, JSON.parse(toolCall.function.arguments))
}Anthropic Tool Use
import { createAnthropicTools } from '@tooly/twilio'
const { tools, executeFunction } = createAnthropicTools('your-account-sid', 'your-auth-token')
// Use with Anthropic client
const message = await anthropic.messages.create({
model: 'claude-3-sonnet-20240229',
messages: [{ role: 'user', content: 'Send a WhatsApp message' }],
tools,
})
// Execute tool calls
for (const toolUse of message.content.filter((c) => c.type === 'tool_use')) {
const result = await executeFunction(toolUse.name, toolUse.input)
}Available Tools
sendSms- Send SMS text messagessendWhatsApp- Send WhatsApp messagesmakeCall- Make phone calls with TwiMLgetCallStatus- Get call status and detailsgetMessageStatus- Get message status and detailslistMessages- List messages with filteringlistCalls- List calls with filteringgetPhoneNumber- Get phone number details and validation
Examples
Send SMS
await twilio.executeFunction('sendSms', {
to: '+1234567890',
from: '+0987654321',
body: 'Hello from Twilio!',
})Make a Call
await twilio.executeFunction('makeCall', {
to: '+1234567890',
from: '+0987654321',
url: 'https://your-server.com/twiml',
})Send WhatsApp Message
await twilio.executeFunction('sendWhatsApp', {
to: 'whatsapp:+1234567890',
from: 'whatsapp:+0987654321',
body: 'Hello via WhatsApp!',
})Get Phone Number Details
await twilio.executeFunction('getPhoneNumber', {
phoneNumber: '+1234567890',
})License
MIT
