Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send by SMS #29

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,25 @@ function nameForHandle(handle) {
}

// Sends a message to the given handle
function send(handle, message) {
function send(handle, message, service = "SMS") {
assert(typeof handle == 'string', 'handle must be a string')
assert(typeof message == 'string', 'message must be a string')
return osa((handle, message) => {
return osa((handle, message, service) => {
const Messages = Application('Messages')

let target

try {
target = Messages.buddies.whose({ handle: handle })[0]
const options = Messages.buddies.whose({ handle: handle })
let chosenIndex = 0;
for (let index = 0; index < options.length; index++) {
const option = options[index];
if (option.service.name().toLowerCase() === service.toLowerCase()) {
chosenIndex = index;
}
}

target = options[chosenIndex];
} catch (e) {}

try {
Expand All @@ -113,7 +122,7 @@ function send(handle, message) {
} catch (e) {
throw new Error(`no thread with handle '${handle}'`)
}
})(handle, message)
})(handle, message, service)
}

let emitter = null
Expand All @@ -138,6 +147,8 @@ function listen() {
SELECT
guid,
id as handle,
message.service,
account,
text,
date,
date_read,
Expand All @@ -158,6 +169,7 @@ function listen() {
guid: msg.guid,
text: msg.text,
handle: msg.handle,
service: msg.service === "SMS" ? msg.service : msg.account,
group: msg.cache_roomnames,
fromMe: !!msg.is_from_me,
date: fromAppleTime(msg.date),
Expand Down