@ria-sys/mcp
Version:
MCP Server para integração com WhatsApp
80 lines (71 loc) • 2.04 kB
text/typescript
export class MessageService {
private apiUrl = 'https://assistants.riasistemas.com.br/v1/notification';
async sendMessage(
toList: string[],
message: string,
sessionName: string
): Promise<{ success: boolean; message: string }> {
try {
const response = await fetch(this.apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
to: toList,
message,
session_name: sessionName
})
});
if (response.ok) {
return {
success: true,
message: '✅ Mensagem enviada com sucesso.'
};
} else {
const errorBody = await response.text().catch(() => '');
return {
success: false,
message: `❌ Erro da API: ${response.status} - ${response.statusText}${errorBody ? ` - ${errorBody.replace(/\s+/g, ' ')}` : ''}`
};
}
} catch (error) {
return {
success: false,
message: `❌ Erro inesperado: ${error instanceof Error ? error.message : 'Erro desconhecido'}`
};
}
}
formatPhone(
phone: string,
removeNinthDigit: boolean = false
): { success: boolean; phone?: string; message?: string } {
try {
if (!phone) {
return {
success: false,
message: '⚠️ Telefone não pode estar vazio.'
};
}
let cleaned = phone.replace(/\D/g, '');
if (!cleaned.startsWith('55')) {
cleaned = '55' + cleaned;
}
if (cleaned.length === 10) {
cleaned = cleaned.slice(0, 4) + '9' + cleaned.slice(4);
}
if (removeNinthDigit && cleaned.length === 11) {
cleaned = cleaned.slice(0, 4) + cleaned.slice(5);
}
return {
success: true,
phone: cleaned
};
} catch (error) {
return {
success: false,
message: `❌ Erro ao formatar: ${error instanceof Error ? error.message : 'Erro desconhecido'}`
};
}
}
}