import axios, { AxiosResponse } from "axios"; const SERVER = "https://app.droidsend.com"; // const API_KEY = "5476b38fff208e0f77c358fad84078b179670ecf"; // export let API_KEY: string | null = null; const USE_SPECIFIED = 0; const USE_ALL_DEVICES = 1; const USE_ALL_SIMS = 2; type ApiResponse = { success: true; data: MessagesArray; error: null; }; type Message = { ID: number; number: string; message: string; deviceID: number; simSlot: string | null; schedule: string | null; userID: number; groupID: string; status: string; resultCode: string | null; errorCode: string | null; type: string; attachments: any | null; prioritize: boolean; retries: number | null; sentDate: string; deliveredDate: string | null; expiryDate: string; }; type MessagesArray = { messages: Message[]; }; interface PostData { [key: string]: any; } async function sendRequest(url: string, postData: PostData): Promise { try { const response: AxiosResponse = await axios.post(url, postData); const data = response.data; if (response.status === 200) { if (data.success) { return data.data; } else { throw new Error(data.error.message); } } else { throw new Error(`HTTP Error Code: ${response.status}`); } } catch (error: any) { throw new Error(error.message); } } export async function sendSingleMessage( API_KEY: string | null = null, number: string, message: string, prioritize: number = 1, device: number = 1490, schedule: string | null = null, isMMS: boolean = false, attachments: any[] | null = null ): Promise { const url = `${SERVER}/services/send.php`; const postData: PostData = { key: API_KEY, number, message, schedule, API_KEY, devices: device, type: isMMS ? "mms" : "sms", attachments, prioritize: prioritize ? 1 : 0, }; if (!API_KEY) { throw new Error("API key not initialized. Please call initApiKey first."); } try { const response = await axios.post(url, null, { params: postData }); const responseData: ApiResponse = response.data; return responseData; } catch (error) { console.log("🚀 ~ error:", error); } } export async function sendMessages( API_KEY: string | null = null, messages: any[], option: number = USE_SPECIFIED, devices: number[] = [], schedule: string | null = null, useRandomDevice: boolean = false ): Promise { const url = `${SERVER}/services/send.php`; const postData: PostData = { messages: JSON.stringify(messages), schedule, key: API_KEY, devices: JSON.stringify(devices), option, useRandomDevice, }; const response = await axios.post(url, null, { params: postData }); const responseData: ApiResponse = response.data; return responseData; } export async function sendMessageToContactsList( API_KEY: string | null = null, listID: string, message: string, option: number = USE_SPECIFIED, devices: number[] = [], schedule: string | null = null, isMMS: boolean = false, attachments: any[] | null = null ): Promise { const url = `${SERVER}/services/send.php`; const postData: PostData = { listID, message, schedule, devices: JSON.stringify(devices), option, key: API_KEY, type: isMMS ? "mms" : "sms", attachments, }; const response = await axios.post(url, null, { params: postData }); const responseData: ApiResponse = response.data; return responseData; } export async function getMessageByID( API_KEY: string | null = null, id: string ): Promise { const url = `${SERVER}/services/read-messages.php`; const postData: PostData = { key: API_KEY, id, }; const response = await axios.post(url, null, { params: postData }); const responseData: ApiResponse = response.data; return responseData; } export async function getMessagesByGroupID( API_KEY: string | null = null, groupID: string ): Promise { const url = `${SERVER}/services/read-messages.php`; const postData: PostData = { key: API_KEY, groupId: groupID, }; const response = await axios.post(url, null, { params: postData }); const responseData: ApiResponse = response.data; return responseData; } export async function getMessagesByStatus( status: string, deviceID: string | null = null, simSlot: number | null = null, startTimestamp: string | null = null, endTimestamp: string | null = null, API_KEY: string | null = null ): Promise { const url = `${SERVER}/services/read-messages.php`; const postData: PostData = { key: API_KEY, status, deviceID, simSlot, startTimestamp, endTimestamp, }; const response = await axios.post(url, null, { params: postData }); const responseData: ApiResponse = response.data; return responseData; } export async function resendMessageByID( API_KEY: string | null = null, id: string ): Promise { const url = `${SERVER}/services/resend.php`; const postData: PostData = { key: API_KEY, id, }; const response = await axios.post(url, null, { params: postData }); const responseData: ApiResponse = response.data; return responseData; } export async function resendMessagesByGroupID( API_KEY: string | null = null, groupID: string, status: string | null = null ): Promise { const url = `${SERVER}/services/resend.php`; const postData: PostData = { key: API_KEY, groupId: groupID, status, }; const response = await axios.post(url, null, { params: postData }); const responseData: ApiResponse = response.data; return responseData; } export async function resendMessagesByStatus( API_KEY: string | null = null, status: string, deviceID: string | null = null, simSlot: number | null = null, startTimestamp: string | null = null, endTimestamp: string | null = null ): Promise { const url = `${SERVER}/services/resend.php`; const postData: PostData = { key: API_KEY, status, deviceID, simSlot, startTimestamp, endTimestamp, }; const response = await axios.post(url, null, { params: postData }); const responseData: ApiResponse = response.data; return responseData; } async function addContact( API_KEY: string | null = null, listID: string, number: string, name: string | null = null, resubscribe: boolean = false ): Promise { const url = `${SERVER}/services/manage-contacts.php`; const postData: PostData = { key: API_KEY, listID, number, name, resubscribe, }; const response = await axios.post(url, null, { params: postData }); const responseData: ApiResponse = response.data; return responseData; } async function unsubscribeContact( listID: string, number: string, API_KEY: string | null = null ): Promise { const url = `${SERVER}/services/manage-contacts.php`; const postData: PostData = { key: API_KEY, listID, number, unsubscribe: true, }; const response = await axios.post(url, null, { params: postData }); const responseData: ApiResponse = response.data; return responseData; } // async function getBalance(): Promise { // const url = `${SERVER}/services/send.php`; // const postData: PostData = { // key: API_KEY, // }; // const credits = await sendRequest(url, postData); // // const response = await axios.post(url, null, { params: postData }); // // const responseData: ApiResponse = response.data; // return credits || "Unlimited"; // } // async function sendUssdRequest( // request: string, // device: number, // simSlot: number | null = null // ): Promise { // const url = `${SERVER}/services/send-ussd-request.php`; // const postData: PostData = { // key: API_KEY, // request, // device, // sim: simSlot, // }; // return await sendRequest(url, postData); // } // async function getUssdRequestByID(id: string): Promise { // const url = `${SERVER}/services/read-ussd-requests.php`; // const postData: PostData = { // key: API_KEY, // id, // }; // return await sendRequest(url, postData); // } // async function getUssdRequests( // request: string, // deviceID: string | null = null, // simSlot: number | null = null, // startTimestamp: string | null = null, // endTimestamp: string | null = null // ): Promise { // const url = `${SERVER}/services/read-ussd-requests.php`; // const postData: PostData = { // key: API_KEY, // request, // deviceID, // simSlot, // startTimestamp, // endTimestamp, // }; // return await sendRequest(url, postData); // } // async function getDevices(): Promise { // const url = `${SERVER}/services/get-devices.php`; // const postData: PostData = { // key: API_KEY, // }; // return await sendRequest(url, postData); // }