@dderevjanik/termux-api
Version:
This library allows you to interact with your Android device from Node.js using termux-api
48 lines (45 loc) • 1.15 kB
text/typescript
import { exec } from "child_process";
export interface SmsSendOptions {
/**
* Numbers Recipient number(s)
* @example ["+421910123456", "0910123456", "4ka"]
*/
numbers: string[];
/**
* Text Text of the SMS
* @example "Hello, World!"
*/
text: string;
/**
* Sim slot to use - silently fails if slot number is invalid or if missing `READ_PHONE_STATE` permission
* @default 0
*/
slot?: number;
}
/**
* Send a SMS message to the specified recipient number(s)
*/
export async function smsSend(options: SmsSendOptions): Promise<void> {
const opts: SmsSendOptions = {
slot: 0,
// number: "",
...options,
};
if (opts.numbers.length === 0) {
throw new Error("Error: No recipient number(s) provided");
}
return new Promise<void>((resolve, reject) => {
const command = `termux-sms-send -n "${opts.numbers.join(",")}" -s ${
opts.slot
} "${opts.text}"`;
exec(command, (error, stdout, stderr) => {
if (error) {
return reject(`Error: ${error.message}`);
}
if (stderr) {
return reject(`Error: ${stderr}`);
}
resolve();
});
});
}