Claude Code के साथ Twilio SMS/ Implementation
Claude Code का उपयोग करके twilio sms/ implementation सीखें। Practical tips और code examples शामिल हैं।
TwilioのSMS/通話featuresको Claude Code सेimplement करना
Twilioはプlogラマブルな通信APIを提供するプラットform है।SMS送信、音声通話、二要素authentication आदि、通信featuresをアプリに組み込め है।Claude Code का उपयोग करके、TwilioのAPIをefficientlyutilizationでき है।
SMS送信の基本
> TwilioでSMS送信するserviceclassを作って。
> templatesupportとリトライprocessingも含めて。
// src/services/sms.ts
import twilio from 'twilio';
const client = twilio(
process.env.TWILIO_ACCOUNT_SID!,
process.env.TWILIO_AUTH_TOKEN!
);
const FROM_NUMBER = process.env.TWILIO_PHONE_NUMBER!;
export class SmsService {
async send(to: string, body: string): Promise<string> {
try {
const message = await client.messages.create({
body,
from: FROM_NUMBER,
to,
});
console.log(`SMS sent: ${message.sid}`);
return message.sid;
} catch (error: any) {
if (error.code === 21211) {
throw new Error('無効な電話番号です');
}
throw error;
}
}
async sendWithRetry(
to: string,
body: string,
maxRetries = 3
): Promise<string> {
for (let i = 0; i < maxRetries; i++) {
try {
return await this.send(to, body);
} catch (error) {
if (i === maxRetries - 1) throw error;
await new Promise(r => setTimeout(r, 1000 * (i + 1)));
}
}
throw new Error('Max retries exceeded');
}
}
二要素authentication(2FA)のimplementation
> Twilio Verifyを使った二要素authenticationをimplement करो。
> codeの送信と検証の両方を作って。
// src/services/verification.ts
import twilio from 'twilio';
const client = twilio(
process.env.TWILIO_ACCOUNT_SID!,
process.env.TWILIO_AUTH_TOKEN!
);
const VERIFY_SERVICE_SID = process.env.TWILIO_VERIFY_SERVICE_SID!;
export class VerificationService {
// authenticationcodeの送信
async sendCode(
to: string,
channel: 'sms' | 'call' | 'email' = 'sms'
): Promise<void> {
await client.verify.v2
.services(VERIFY_SERVICE_SID)
.verifications.create({
to,
channel,
});
console.log(`Verification code sent to ${to} via ${channel}`);
}
// authenticationcodeの検証
async verifyCode(to: string, code: string): Promise<boolean> {
try {
const check = await client.verify.v2
.services(VERIFY_SERVICE_SID)
.verificationChecks.create({
to,
code,
});
return check.status === 'approved';
} catch (error: any) {
if (error.code === 20404) {
return false; // codeが期限切れ
}
throw error;
}
}
}
APIendpointのimplementation
// src/api/auth/verify.ts
import { VerificationService } from '../../services/verification';
const verifyService = new VerificationService();
// code送信
export async function POST(req: Request) {
const { phone } = await req.json();
if (!phone) {
return Response.json({ error: '電話番号がज़रूरीです' }, { status: 400 });
}
try {
await verifyService.sendCode(phone);
return Response.json({ success: true, message: 'authenticationcodeを送信しました' });
} catch (error) {
return Response.json({ error: '送信に失敗しました' }, { status: 500 });
}
}
// code検証
export async function PUT(req: Request) {
const { phone, code } = await req.json();
const isValid = await verifyService.verifyCode(phone, code);
if (isValid) {
return Response.json({ success: true, message: 'authentication成功' });
}
return Response.json({ error: '無効なcodeです' }, { status: 400 });
}
通知システムのbuild
> 注文ステータス変更時にSMS通知を送る仕組みを作って。
// src/services/notification.ts
import { SmsService } from './sms';
const smsService = new SmsService();
const TEMPLATES = {
ORDER_CONFIRMED: (orderId: string) =>
`ご注文 #${orderId} を承り हुआ।準備が整いअगला第、発送いたし है।`,
ORDER_SHIPPED: (orderId: string, trackingUrl: string) =>
`ご注文 #${orderId} を発送し हुआ।配送状況: ${trackingUrl}`,
ORDER_DELIVERED: (orderId: string) =>
`ご注文 #${orderId} が配達され हुआ।ご利用ありがとうござい है।`,
};
export async function notifyOrderStatus(
phone: string,
orderId: string,
status: 'confirmed' | 'shipped' | 'delivered',
metadata?: { trackingUrl?: string }
) {
let message: string;
switch (status) {
case 'confirmed':
message = TEMPLATES.ORDER_CONFIRMED(orderId);
break;
case 'shipped':
message = TEMPLATES.ORDER_SHIPPED(orderId, metadata?.trackingUrl || '');
break;
case 'delivered':
message = TEMPLATES.ORDER_DELIVERED(orderId);
break;
}
await smsService.sendWithRetry(phone, message);
}
Summary
Twilioの通信APIको Claude Code सेefficientlyimplementationし、SMS通知や二要素authenticationを素早くアプリに組み込め है।authenticationimplementationガイドやWebhookimplementationも合わせてreference के लिए देखें。
Twilioके details के लिएTwilioofficial documentationをदेखें。
Related Posts
Claude Code से अपने Side Projects को Supercharge कैसे करें [Examples के साथ]
Claude Code से personal development projects को dramatically speed up करना सीखें। Real-world examples और idea से deployment तक practical workflow शामिल है।
Claude Code से Refactoring कैसे Automate करें
Claude Code से efficiently code refactoring automate करना सीखें। Real-world projects के लिए practical prompts और concrete refactoring patterns शामिल हैं।
Claude Code के साथ Complete CORS Configuration Guide
Claude Code का उपयोग करके complete CORS configuration guide सीखें। Practical tips और code examples शामिल हैं।