@dderevjanik/termux-api
Version:
This library allows you to interact with your Android device from Node.js using termux-api
106 lines (96 loc) • 2.14 kB
text/typescript
import { exec } from "child_process";
export interface Message {
/**
* The ID of the thread
* @example 109
*/
threadid: number;
/**
* The type of the message
* @example "inbox"
*/
type: string;
/**
* The read status of the message
* @example false
*/
read: boolean;
/**
* The number associated with the message
* @example "950"
*/
number: string;
/**
* The received timestamp of the message
* @example "2024-12-19 17:15:40"
*/
received: string;
/**
* The body content of the message
* @example "Nechajte starosti plavat a zdielajte bezstarostne Vianoce s rodinou a nekonecnymi datami"
*/
body: string;
/**
* The unique ID of the message
* @example 1016
*/
_id: number;
}
export interface SmsListOptions {
/**
* limit in retrieved sms list
* @default 10
*/
limit?: number;
/**
* offset in sms list
* @default 0
*/
offset?: number;
/**
* the type of messages to list
* @default "all"
*/
type?: "all" | "inbox" | "sent" | "draft" | "outbox";
/**
* the number for locate messages
* @example "950"
*/
// number?: string;
}
/**
* List sms messages
*
* **Requires permission:** `android.permission.READ_SMS` and `android.permission.READ_CONTACTS`
*/
export async function smsList(
options: SmsListOptions = {}
): Promise<Message[]> {
const opts: SmsListOptions = {
limit: 10,
offset: 0,
type: "all",
...options,
};
return new Promise<Message[]>((resolve, reject) => {
const command = `termux-sms-list -l ${opts.limit} -o ${opts.offset} -t ${opts.type}`;
exec(command, (error, stdout, stderr) => {
if (error) {
return reject(`Error: ${error.message}`);
}
if (stderr) {
return reject(`Error: ${stderr}`);
}
const output = stdout.trim();
if (output.length === 0) {
return reject(`Error: No messages found`);
}
try {
const messages = JSON.parse(output);
return resolve(messages);
} catch (e: any) {
return reject(`Error: ${e.message}`);
}
});
});
}