forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendSms.ts
173 lines (146 loc) · 5.94 KB
/
sendSms.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import {
ActionExample,
generateText,
HandlerCallback,
IAgentRuntime,
Memory,
ModelClass,
State,
type Action,
} from "@elizaos/core";
import twilio from 'twilio';
export const sendSmsAction: Action = {
name: "SendSms",
similes: [
"SendSms"
],
validate: async (_runtime: IAgentRuntime, _message: Memory) => {
return true;
},
description:
"Send SMS to the mobile number provided by the user",
handler: async (
_runtime: IAgentRuntime,
_message: Memory,
_state: State,
_options:{[key:string]: unknown},
_callback: HandlerCallback,
): Promise<boolean> => {
// Check if environment variables are set
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
console.log("CHECK _message: ",_message.content.text);
if (!accountSid || !authToken) {
console.error('TWILIO_ACCOUNT_SID or TWILIO_AUTH_TOKEN is not set');
return false;
}
// Extract the mobile number from the message
const mobileNumberRegex = /(?:\+|00)(\d{1,3})\s?(\d{3,5})\s?(\d{4,10})/; // This regex matches numbers like +1 123 4567890 or 001 123 4567890
const text = (_message.content as { text?: string })?.text || '';
const matches = text.match(mobileNumberRegex);
const messageRegex = /(['"])(.*?)\1/;
const messageMatch = text.match(messageRegex);
let mobileNumberProvidedByUser = null;
let messageToSendFromUser = null;
if(messageMatch){
messageToSendFromUser = messageMatch[2];
}
if (matches) {
// Combine the parts of the number into a single string, removing spaces and plus signs
mobileNumberProvidedByUser = `+${matches[1]}${matches[2]}${matches[3]}`;
}else{
const alternativeMobileNumberRegex = /\b(\d{3})[-.]?(\d{3})[-.]?(\d{4})\b/; // For formats like 123-456-7890 or 123.456.7890
if (!mobileNumberProvidedByUser) {
const alternativeMatches = text.match(alternativeMobileNumberRegex);
if (alternativeMatches) {
mobileNumberProvidedByUser = `${alternativeMatches[1]}${alternativeMatches[2]}${alternativeMatches[3]}`;
}
}
}
const twilioNumber = process.env.TWILIO_PHONE_NUMBER; // Your Twilio phone number
console.log('check target mobile number: ', mobileNumberProvidedByUser);
console.log('check messageToSendFromUser: ', messageToSendFromUser);
console.log('check twilioNumber: ', twilioNumber);
if (!twilioNumber) {
console.error('Twilio phone number is missing');
_callback({
text: `Sorry there was an issue send sms, please try again later`,
});
return false;
}
const recentMessages = `Extract the phone number from the user recent messages ${_state.recentMessages}`;
if (!mobileNumberProvidedByUser) {
console.error('Mobile number is missing will try to get the phone number or mobile number from recent messages');
mobileNumberProvidedByUser = await generateText(
{
runtime: _runtime,
context: recentMessages,
modelClass: ModelClass.SMALL,
stop: ["\n"],
customSystemPrompt: "only extract the message that the user intend to send and only get the last one"
}
);
}
if (!mobileNumberProvidedByUser) {
console.error('Mobile number is missing');
_callback({
text: `Sorry, there was an issue send sms, please try again later`,
});
return false;
}
const recentUserMessages = `Extract the message intended for SMS or text: ${_state.recentMessages}`;
if (!messageToSendFromUser) {
console.error('messageToSendFromUser is missing will try to get the user message from recent messages');
messageToSendFromUser = await generateText(
{
runtime: _runtime,
context: recentUserMessages,
modelClass: ModelClass.SMALL,
stop: ["\n"]
}
);
}
if(messageToSendFromUser==null){
console.error('messageToSendFromUser is empty or null');
_callback({
text: `Sorry there was an issue sending the WhatsApp message, please try again later`,
});
return false;
}
try {
// Initialize Twilio client
const client = twilio(accountSid, authToken);
// Send the SMS
const message= await client.messages.create({
body: messageToSendFromUser, // The message body
to: mobileNumberProvidedByUser, // The recipient's phone number
from: twilioNumber, // Your Twilio phone number
});
console.log("message body: ", message);
const messageFromAgent = `SMS sent successfully to ${mobileNumberProvidedByUser}`;
// Call the callback to notify the user
_callback({
text: messageFromAgent,
});
return true;
} catch (error) {
console.error('Failed to send SMS:', error);
_callback({
text: `Failed to send SMS to ${mobileNumberProvidedByUser}`,
});
return false;
}
},
examples: [
[
{
user: "{{user1}}",
content: { text: "please send my message via sms to target mobile number" },
},
{
user: "{{user2}}",
content: { text: "", action: "SEND_SMS" },
},
],
] as ActionExample[][],
} as Action;