Code Monkey home page Code Monkey logo

osa-imessage's Introduction

osa-imessage

styled with prettier

Send and receive iMessages through nodejs

Installation

Requires OSX 10.10 Yosemite

npm install osa-imessage --save

Usage

Be sure to require osa-imessage:

const imessage = require('osa-imessage')

Send a message

imessage.send('+15555555555', 'Hello World!')

Receive messages

imessage.listen().on('message', (msg) => {
    if (!msg.fromMe) console.log(`'${msg.text}' from ${msg.handle}`)
})

Send message to name

imessage.handleForName('Tim Cook').then(handle => {
    imessage.send(handle, 'Hello')
})

Send message to group

imessage.send('chat000000000000000000', 'Hello everyone!')

Get recent chats

imessage.getRecentChats(20) // Defaults to 10

API

Send a message

send(handle, text) -> Promise

Sends a message to the specified handle.

handle

Type: string

The user or group to send the message to, in one of the following forms:

  • phone number (+1555555555)
  • email address ([email protected])
  • group chat id (chat000000000000000000)

text

Type: string

The content of the message to be sent.

return

Type: Promise<>

A promise that resolves when the message is sent, and rejects if the message fails to send.

Receive messages

listen([interval]) -> EventEmitter

Begins polling the local iMessage database for new messages.

interval

Type: number

The rate at which the database is polled for new messages. Defaults to the minimum of 1000 ms.

return

Type: EventEmitter

An event emitter with that listeners can be attached to. Currently it only has the message event.

Example message event

{
    text: 'Hello, world!',
    handle: '+15555555555',
    group: null,
    date: new Date('2017-04-11T02:02:13.000Z'),
    fromMe: false,
    guid: 'F79E08A5-4314-43B2-BB32-563A2BB76177'
}

Example group message event

{
    text: 'Hello, group!',
    handle: '+15555555555',
    group: 'chat000000000000000000',
    date: new Date('2017-04-23T21:18:54.943Z'),
    fromMe: false,
    guid: 'DCFE0EEC-F9DD-48FC-831B-06C75B76ACB9'
}

Get a handle for a given name

handleForName(name) -> Promise<handle>

name

Type: string

The full name of the desired contact, as displayed in Messages.app.

return

Type: Promise<string>

A promise that resolves with the handle of the contact, or rejects if nobody was found.

Get the name associated with a given handle

nameForHandle(handle) -> Promise<name>

handle

Type: string

The handle of a contact.

return

Type: Promise<string>

A promise that resolved with the full name of the desired contact, as displayed in Messages.app.

Get recents chats

getRecentChats(limit) -> Promise

limit

Type: integer

Amount of recent chats to return.

return

Type: Promise

A promise that resolves with an array of chats.

osa-imessage's People

Contributors

bradylf avatar briangonzalez avatar irfaan avatar omgimalexis avatar wtfaremyinitials avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

osa-imessage's Issues

Unable to send text

using the example from the readme in file:

const imessage = require('osa-imessage');
imessage.send('+<mycellnumber>', 'Hello World!')

fails with this error:

svens-mbp:osa-test svenyonson$ node test.js

(node:21987) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 
1): Error: Command failed: /usr/bin/osascript -l JavaScript
445:509: execution error: Error on line 19: Error: no thread with handle '+<mycellnumber>' (-2700)

(node:21987) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In 
the future, promise rejections that are not handled will terminate the Node.js process with a 
non-zero exit code.

Can't get this to work!

I am getting the following error:

undefined:1
��{
^
SyntaxError: Unexpected token �
    at Object.parse (native)
    at LineStream.<anonymous> (/[PATH]/node_modules/osa-imessage/index.js:45:21)
    at LineStream.emit (events.js:95:17)
    at LineStream.<anonymous> (_stream_readable.js:764:14)
    at LineStream.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:426:10)
    at emitReadable (_stream_readable.js:422:5)
    at readableAddChunk (_stream_readable.js:165:9)
    at LineStream.Readable.push (_stream_readable.js:127:10)
    at LineStream.Transform.push (_stream_transform.js:140:32)

Related to this? jahewson/node-byline#17

I even tried restricting byline to v 4.1.1
On node v.0.10.31 (the latest version threw errors) and same error on node v.0.10.3 (see the issue linked above).

Any ideas?

EDIT
Hacked around for a temporary solution:

var re = /\0/g;
var data = JSON.parse(data.toString().substr(2).replace(re, ""));

Cant send messages on macOS (14.2.0)

I was using apple script to send messages before this but those scripts are seemingly broken as well. Anybody know what might have changed? This is the error I got when running the echo.js (I censored the phone number)

(node:29344) UnhandledPromiseRejectionWarning: Error: Command failed: /usr/bin/osascript -l JavaScript
execution error: Error: Error: no thread with handle '+14xxxxxxxx1' (-2700)

    at ChildProcess.exithandler (child_process.js:308:12)
    at ChildProcess.emit (events.js:314:20)
    at maybeClose (internal/child_process.js:1021:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5)
(node:29344) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:29344) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

NodeNoob - nameForHandle returns "[object Promise]"

Okay, sorry to bother you (because this is probably stupidly obvious and I don't mean to clog your issues with questions about the intricacies of Node), but when I run this:

imessage.listen().on('message', (msg) => {
  if (msg) {
    console.log(`${imessage.nameForHandle(msg.handle)} said hi`)
  }
})

It logs [object Promise] said hi when I receive a message, but I would obviously like it to log Bob said hi.
I can't figure it out, any help would be appreciated, thanks.

Feature request: support sending file attachments

I love the repo! Thanks for making it.

Since iMessage can handle sending files, it would be nice if this package could handle sending files as well.

I've made a PR that gives the export a sendFile method that accepts a handle and an absolute filepath. Let me know what you think!

Cheers,

Elliot

Is this still active?

I get errors trying to install on Mac and this project is a bit dated so I'm curious if there's hope to make this work.

UnhandledPromiseRejectionWarning when sending message

When running this script:

const iMessage = require('osa-imessage');
iMessage.send('number redacted', 'Hello World');

I get this output in the terminal:

This version of macOS (10.11.6) is currently untested with this version of osa-imessage. Proceed with caution.
(node:644) UnhandledPromiseRejectionWarning: Error: Command failed: /usr/bin/osascript -l JavaScript
31:73: syntax error: Error on line 3: SyntaxError: Unexpected token '>' (-2700)

    at ChildProcess.exithandler (child_process.js:275:12)
    at emitTwo (events.js:126:13)
    at ChildProcess.emit (events.js:214:7)
    at maybeClose (internal/child_process.js:925:16)
    at Socket.stream.socket.on (internal/child_process.js:346:11)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at Pipe._handle.close [as _onclose] (net.js:567:12)
(node:644) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:644) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code

I am running Node v8.9.4 and OS X 10.11.6.

Can't read messages on Mojave 10.14.2

Hi,
I'm using Mojave 10.14.2 and i can't read messages.
I get this error:
(node:12727) UnhandledPromiseRejectionWarning: Error: SQLITE_CANTOPEN: unable to open database file (node:12727) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:12727) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I'm using the example about imessage.listen().on("message")

Seems very broken on latest macOS (12.4)

    at Database.all (/Users/mike/comp/js/nophone/node_modules/sqlite3/lib/sqlite3.js:18:25)
    at /Users/mike/comp/js/nophone/node_modules/sqlite/main.js:248:19
    at new Promise (<anonymous>)
    at Database.all (/Users/mike/comp/js/nophone/node_modules/sqlite/main.js:247:12)
    at Timeout.check [as _onTimeout] (/Users/mike/comp/js/nophone/node_modules/osa-imessage/index.js:153:39)```

This is AFTER granting terminal + node access to the filesystem.

Previously working implementation (Late '15) now breaks.

Hi - I don't think you're maintaining this anymore but in case you are: I had this plugin working in Nov 2015 - no change has been made to my local implementation but it's stopped working sometime since then.

{ [Error: Command failed: /bin/sh -c osascript -l JavaScript -e 'var old = console.log; console.log = function () { Array.prototype.unshift.call(arguments, "<brandonhorst:node-osa>"); Array.prototype.push.call(arguments, "</brandonhorst:node-osa>"); old.apply(console, arguments); }; JSON.stringify((function (message, recipient) {        var Messages = Application('\''Messages'\'');        recipient = Messages.buddies.whose(recipient)[0];        Messages.send(message, { to: recipient });        return {            name:   recipient.name(),            handle: recipient.handle()        };    })("hi",{"name":"NUMBER_REPLACED"}));'
0:558: execution error: Error on line 1: Error: Can't get object. (-1728)
]
  killed: false,
  code: 1,
  signal: null,
  cmd: '/bin/sh -c osascript -l JavaScript -e \'var old = console.log; console.log = function () { Array.prototype.unshift.call(arguments, "<brandonhorst:node-osa>"); Array.prototype.push.call(arguments, "</brandonhorst:node-osa>"); old.apply(console, arguments); }; JSON.stringify((function (message, recipient) {        var Messages = Application(\'\\\'\'Messages\'\\\'\');        recipient = Messages.buddies.whose(recipient)[0];        Messages.send(message, { to: recipient });        return {            name:   recipient.name(),            handle: recipient.handle()        };    })("hi",{"name":"NUMBER_REPLACED"}));\'' }

I haven't had time to dig into the code but let me know if there's a solution - I'd happily submit a PR.

Cannot listen for messages on 10.15.4

Sending messages is working great.

But the listening example code:

const imessage = require("osa-imessage");

imessage.listen().on("message", (msg) => {
  console.log(msg);
});

simply exits. Am I missing something?

~/dev/osa|⇒  node test.js
This version of macOS (13.4.0) is currently untested with this version of osa-imessage. Proceed with caution.
~/dev/osa|⇒

cannot find module error

the error is: Error: Cannot find module '/Users/vektrix/node_modules/sqlite3/lib/binding/node-v79-darwin-x64/node_sqlite3.node'

the require stack is:

    '/Users/vektrix/node_modules/sqlite3/lib/sqlite3.js',
    '/Users/vektrix/node_modules/sqlite/main.js',
    '/Users/vektrix/node_modules/osa-imessage/lib/messages-db.js',
    '/Users/vektrix/node_modules/osa-imessage/index.js',
    '/Users/vektrix/iMessage/index.js'
  ]

What is the `Application` function?

This is quite possibly a dumb question, but it seems that everything boils down to the Application function here.

My question is, where is that function defined? Where is it documented? Is it built it?

I can't find anything on it, but that could be because I don't know where to look.

Move to async/await and node-sqlite

node-sqlite is a promise-ified wrapper over sqlite3.

Would you be ok with me refactoring osa-imessage to use async/await and node-sqlite? I think it would clean up the code quite a bit. I am also hoping to add in a mock sqlite db and use it to power a small unit testing suite.

Thoughts?

can't install on macOS 12.3 (Monterey)

when I try to run npm install osa-imessage, I am faced with this wall of errors:

npm WARN deprecated [email protected]: Please upgrade to @mapbox/node-pre-gyp: the non-scoped node-pre-gyp package is deprecated and only the @mapbox scoped package will recieve updates in the future
npm ERR! code 1
npm ERR! path /Users/orenlindsey/Developer/Express-Example-App-1/node_modules/sqlite3
npm ERR! command failed
npm ERR! command sh -c node-pre-gyp install --fallback-to-build
npm ERR! ACTION deps_sqlite3_gyp_action_before_build_target_unpack_sqlite_dep Release/obj/gen/sqlite-autoconf-3310100/sqlite3.c
npm ERR! Failed to execute '/usr/local/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js build --fallback-to-build --module=/Users/orenlindsey/Developer/Express-Example-App-1/node_modules/sqlite3/lib/binding/node-v93-darwin-x64/node_sqlite3.node --module_name=node_sqlite3 --module_path=/Users/orenlindsey/Developer/Express-Example-App-1/node_modules/sqlite3/lib/binding/node-v93-darwin-x64 --napi_version=8 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v93' (1)
npm ERR! node-pre-gyp info it worked if it ends with ok
npm ERR! node-pre-gyp info using [email protected]
npm ERR! node-pre-gyp info using [email protected] | darwin | x64
npm ERR! node-pre-gyp WARN Using needle for node-pre-gyp https download 
npm ERR! node-pre-gyp info check checked for "/Users/orenlindsey/Developer/Express-Example-App-1/node_modules/sqlite3/lib/binding/node-v93-darwin-x64/node_sqlite3.node" (not found)
npm ERR! node-pre-gyp http GET https://mapbox-node-binary.s3.amazonaws.com/sqlite3/v4.2.0/node-v93-darwin-x64.tar.gz
npm ERR! node-pre-gyp http 403 https://mapbox-node-binary.s3.amazonaws.com/sqlite3/v4.2.0/node-v93-darwin-x64.tar.gz
npm ERR! node-pre-gyp WARN Tried to download(403): https://mapbox-node-binary.s3.amazonaws.com/sqlite3/v4.2.0/node-v93-darwin-x64.tar.gz 
npm ERR! node-pre-gyp WARN Pre-built binaries not found for [email protected] and [email protected] (node-v93 ABI, unknown) (falling back to source compile with node-gyp) 
npm ERR! node-pre-gyp http 403 status code downloading tarball https://mapbox-node-binary.s3.amazonaws.com/sqlite3/v4.2.0/node-v93-darwin-x64.tar.gz 
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using [email protected]
npm ERR! gyp info using [email protected] | darwin | x64
npm ERR! gyp info ok 
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using [email protected]
npm ERR! gyp info using [email protected] | darwin | x64
npm ERR! gyp info find Python using Python version 3.8.9 found at "/Applications/Xcode.app/Contents/Developer/usr/bin/python3"
npm ERR! gyp info spawn /Applications/Xcode.app/Contents/Developer/usr/bin/python3
npm ERR! gyp info spawn args [
npm ERR! gyp info spawn args   '/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py',
npm ERR! gyp info spawn args   'binding.gyp',
npm ERR! gyp info spawn args   '-f',
npm ERR! gyp info spawn args   'make',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/Users/orenlindsey/Developer/Express-Example-App-1/node_modules/sqlite3/build/config.gypi',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/usr/local/lib/node_modules/npm/node_modules/node-gyp/addon.gypi',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/Users/orenlindsey/Library/Caches/node-gyp/16.14.2/include/node/common.gypi',
npm ERR! gyp info spawn args   '-Dlibrary=shared_library',
npm ERR! gyp info spawn args   '-Dvisibility=default',
npm ERR! gyp info spawn args   '-Dnode_root_dir=/Users/orenlindsey/Library/Caches/node-gyp/16.14.2',
npm ERR! gyp info spawn args   '-Dnode_gyp_dir=/usr/local/lib/node_modules/npm/node_modules/node-gyp',
npm ERR! gyp info spawn args   '-Dnode_lib_file=/Users/orenlindsey/Library/Caches/node-gyp/16.14.2/<(target_arch)/node.lib',
npm ERR! gyp info spawn args   '-Dmodule_root_dir=/Users/orenlindsey/Developer/Express-Example-App-1/node_modules/sqlite3',
npm ERR! gyp info spawn args   '-Dnode_engine=v8',
npm ERR! gyp info spawn args   '--depth=.',
npm ERR! gyp info spawn args   '--no-parallel',
npm ERR! gyp info spawn args   '--generator-output',
npm ERR! gyp info spawn args   'build',
npm ERR! gyp info spawn args   '-Goutput_dir=.'
npm ERR! gyp info spawn args ]
npm ERR! gyp info ok 
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using [email protected]
npm ERR! gyp info using [email protected] | darwin | x64
npm ERR! gyp info spawn make
npm ERR! gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
npm ERR! /bin/sh: python: command not found
npm ERR! make: *** [Release/obj/gen/sqlite-autoconf-3310100/sqlite3.c] Error 127
npm ERR! gyp ERR! build error 
npm ERR! gyp ERR! stack Error: `make` failed with exit code: 2
npm ERR! gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:194:23)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:526:28)
npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:291:12)
npm ERR! gyp ERR! System Darwin 21.4.0
npm ERR! gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "build" "--fallback-to-build" "--module=/Users/orenlindsey/Developer/Express-Example-App-1/node_modules/sqlite3/lib/binding/node-v93-darwin-x64/node_sqlite3.node" "--module_name=node_sqlite3" "--module_path=/Users/orenlindsey/Developer/Express-Example-App-1/node_modules/sqlite3/lib/binding/node-v93-darwin-x64" "--napi_version=8" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v93"
npm ERR! gyp ERR! cwd /Users/orenlindsey/Developer/Express-Example-App-1/node_modules/sqlite3
npm ERR! gyp ERR! node -v v16.14.2
npm ERR! gyp ERR! node-gyp -v v8.4.1
npm ERR! gyp ERR! not ok 
npm ERR! node-pre-gyp ERR! build error 
npm ERR! node-pre-gyp ERR! stack Error: Failed to execute '/usr/local/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js build --fallback-to-build --module=/Users/orenlindsey/Developer/Express-Example-App-1/node_modules/sqlite3/lib/binding/node-v93-darwin-x64/node_sqlite3.node --module_name=node_sqlite3 --module_path=/Users/orenlindsey/Developer/Express-Example-App-1/node_modules/sqlite3/lib/binding/node-v93-darwin-x64 --napi_version=8 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v93' (1)
npm ERR! node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (/Users/orenlindsey/Developer/Express-Example-App-1/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
npm ERR! node-pre-gyp ERR! stack     at ChildProcess.emit (node:events:526:28)
npm ERR! node-pre-gyp ERR! stack     at maybeClose (node:internal/child_process:1092:16)
npm ERR! node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:302:5)
npm ERR! node-pre-gyp ERR! System Darwin 21.4.0
npm ERR! node-pre-gyp ERR! command "/usr/local/bin/node" "/Users/orenlindsey/Developer/Express-Example-App-1/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
npm ERR! node-pre-gyp ERR! cwd /Users/orenlindsey/Developer/Express-Example-App-1/node_modules/sqlite3
npm ERR! node-pre-gyp ERR! node -v v16.14.2
npm ERR! node-pre-gyp ERR! node-pre-gyp -v v0.11.0
npm ERR! node-pre-gyp ERR! not ok

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/orenlindsey/.npm/_logs/2022-05-10T21_05_50_231Z-debug-0.log

(the complete log is here)

At first, I thought it had something to do with the sqlite3 or sqlite library, but I was able to install both of those individually without any problems. So now I think it is a problem with python in some way, but I'm not sure how to fix it

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.