wtfaremyinitials / osa-imessage

Send and receive iMessages with nodejs
MIT License
324 stars 54 forks source link

Previously working implementation (Late '15) now breaks. #4

Closed tomhardman0 closed 8 years ago

tomhardman0 commented 8 years ago

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.

wtfaremyinitials commented 8 years ago

I think Apple changed the way that API works, since I haven't touched this code in a while (obviously). I'm in the middle of finals right now, but I can take a stab at updating this next week.

tomhardman0 commented 8 years ago

No problem - I ended up writing a very simple cli for imessaging/sms instead.

Good luck with your finals!

P.S. After reading around, it seems Apple removed the API for this in OSX 10.11!

liamnichols commented 8 years ago

I don't think the API has been removed..

I can still run this script in Script Editor:

var Messages = Application('Messages');
var recipient = Messages.buddies.whose({ handle: '<PHONE NUMBER>' })[0];
Messages.send('Hello?', { to: recipient });

And it works..

Try running imessage.send('Hello?', '<PHONE NUMBER>', callback); and i get

execution error: Error on line 1: Error: Can't get object. (-1728)

Any ideas?

liamnichols commented 8 years ago

Turns out it is something to do with the + character that gets trimmed off the phone number? "Can't get object." means that it can't find the buddy.

A work around is to pass the recipient object in as a whole. I.e:

imessage.send('Hello?', { handle: '+447000000000' }, callback);
wtfaremyinitials commented 8 years ago

It was the receiving messages API that got removed. Even Apple's official example of it is broken.

I wasn't aware that sending messages was broken as well. I'll take a look at that next week. (Funny how this project always gets attention during finals weeks)

liamnichols commented 8 years ago

Ah! To be honest, I thought this issue was about sending 🙄 Thanks for the info though and good luck with your finals!

liamnichols commented 8 years ago

Just an update for whoever might find this useful.. I noticed that the issue I am seeing is when trying to send a message to somebody where there is not yet a conversation in the Messages app.

I have however found another script from PieMessage that also sends a "service id" in the send command?

I don't know anything about applescript so was struggling to convert it into the osa javascript implementation (only just started playing with js recently too)...

Here is the script that does work anyway:

on run {msgText, targetPhoneNum}
    tell application "Messages"
        set serviceID to id of 1st service whose service type = iMessage
        send msgText to buddy targetPhoneNum of service id serviceID
    end tell
end run

(src)

It will probably be useful to somebody who can convert it into the js implementation and then fix any issues with this project..

I was trying to do something on the lines of var service = Messages.services.whose({ serviceType: 'iMessage' })[0];

but was running into issues when trying to pass it into Messages.send()

hope it helps :)

wtfaremyinitials commented 8 years ago

Sorry to leave you all hanging. I'm investigating the issues now.

wtfaremyinitials commented 8 years ago

Okay so I'm pretty sure I have the sending messages issue resolved. The receiving messages one could use some more looking in to

wtfaremyinitials commented 8 years ago

I'm fairly certain that Apple has stopped supporting the message handler Apple Script, meaning without major shifts in the way the code works, receiving messages will not be possible.

Using Apple's example script for the API yields this error.

If anyone finds new issues or discovers a solution to the receiving messages, feel free to open a new issue or PR.