Skip to content

Commit

Permalink
fix whastapp and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
sudtanj committed Dec 7, 2024
1 parent 74651af commit 587e4b2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/callmebot_whatsapp_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@
* https://www.callmebot.com/blog/free-api-whatsapp-messages/
* - Make sure your Google Apps Script project has the necessary permissions (like UrlFetch) enabled.
*/
class WhatsAppAPI {
class CallMeBotWhatsAppAPI {
/**
* Sends a WhatsApp text message to the provided phone number.
*
* @param phoneNumber The recipient's phone number in international format (e.g., +6281854378145).
* @param message The text message to be sent.
* @throws Error if the API key is not found in script properties or an error occurs during the API request.
*/
public static async sendMessage(phoneNumber: string, message: string): Promise<void> {
public static async sendMessage(message: string): Promise<void> {
const apiKey = PropertiesService.getScriptProperties().getProperty('call_me_bot_api_key');
if (!apiKey) {
throw new Error('API key not found in script properties. Please set the "call_me_bot_api_key" property.');
}

const targetPhoneNumber = PropertiesService.getScriptProperties().getProperty('call_me_bot_target_phone');
if (!targetPhoneNumber) {
throw new Error('Target phone number not found in script properties. Please set the "call_me_bot_target_phone" property.');
}

const url = `https://api.callmebot.com/whatsapp.php?phone=${phoneNumber}&text=${message}&apikey=${apiKey}`;

try {
Expand Down
3 changes: 3 additions & 0 deletions test/test_send_whastapp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function TestSendWhatsapp() {
CallMeBotWhatsAppAPI.sendMessage("Test Send Whatsapp");
}

0 comments on commit 587e4b2

Please sign in to comment.