-
-
Save adrum/e33f5f240fecc7e41c3d90325d0fce6d to your computer and use it in GitHub Desktop.
A simple bridge between iMessage and Home Assistant's conversation component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const HomeAssistant = require( 'homeassistant' ); | |
| const Pino = require( 'pino' ); | |
| const config = require( 'config' ); | |
| const hass = new HomeAssistant( config.get( 'home_assistant' ) ); | |
| const imessage = require( 'osa-imessage' ); | |
| const logger = Pino(); | |
| // TODO package this better | |
| const bridge = { | |
| handleMessage: function( sender, text ) { | |
| logger.info( `from ${sender}: ${text}` ); | |
| hass.services._post( '/conversation/process', null, { | |
| text: text | |
| } ) | |
| .then( res => { | |
| imessage.send( sender, res.speech.plain.speech ); | |
| } ) | |
| .catch( err => { | |
| logger.error( err ); | |
| imessage.send( sender, "Sorry, something broke" ); | |
| } ); | |
| } | |
| } | |
| imessage.listen().on( 'message', ( msg ) => { | |
| if ( msg.fromMe ) { | |
| logger.warn( `ignoring message from myself: ${msg.text}` ); | |
| return; | |
| } | |
| if ( ! config.get( 'allowed_senders' ).includes( msg.handle ) ) { | |
| logger.warn( `ignoring message from unknown sender: ${msg.handle}: ${msg.text}` ); | |
| return; | |
| } | |
| bridge.handleMessage( msg.handle, msg.text ); | |
| } ); | |
| // Setup notify service | |
| if (config.notify.port) { | |
| const express = require('express'); | |
| const app = express(); | |
| app.use(express.json()); | |
| app.post('/notify', async function(req, res){ | |
| const to = req.body.to | |
| let message = req.body.message; | |
| if (req.body.title) message = `${req.body.title}\n${message}` | |
| // Check auth | |
| if (config.notify.token) { | |
| const token = (req.body.token || req.query.token || req.headers['authorization'] || '').replace('Bearer ', '').replace('Token ', ''); | |
| if (token != config.notify.token) { | |
| logger.error( `unauthorized send: ${message} : ${e}` ); | |
| return res.status(403).json({error: 'Unauthorized'}); | |
| } | |
| } | |
| logger.info(`${JSON.stringify(req.body)}`) | |
| // Send to imessage | |
| try { | |
| await imessage.send( to, message ); | |
| logger.error( `sent message: ${to} : ${message}` ); | |
| } catch(e) { | |
| logger.error( `error sending: ${message} : ${e}` ); | |
| return res.status(500).json({error: 'iMessage Error'}); | |
| } | |
| res.status(200).json({status: true}) | |
| }) | |
| var server = app.listen(config.notify.port, function () { | |
| var host = server.address().address | |
| var port = server.address().port | |
| console.log("Home Assistant iMessage Notify Service listening at http://%s:%s", host, port) | |
| }) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| allowed_senders: | |
| - "me@example.com" | |
| home_assistant: | |
| host: "http://<your home assistant host or ip>" | |
| port: 8123 | |
| token: "<your home assistant token>" | |
| notify: | |
| port: 8124 | |
| token: "<your imessage notify token>" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Include this in your Home Assistant configuration.yaml | |
| notify: | |
| - name: imessage | |
| platform: rest | |
| resource: http://<your mac ip or hostname>:8124/notify | |
| method: 'POST_JSON' | |
| headers: | |
| Authorization: Token <your imessage notify token> | |
| title_param_name: 'title' | |
| data_template: | |
| to: '+15551236789' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "imessage-home-assistant", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "app.js", | |
| "scripts": { | |
| "start": "node app.js", | |
| "test": "echo \"Error: no test specified\" && exit 1" | |
| }, | |
| "author": "", | |
| "license": "MIT", | |
| "dependencies": { | |
| "config": "3.0.1", | |
| "express": "^4.16.4", | |
| "homeassistant": "0.2.0", | |
| "js-yaml": "3.12.1", | |
| "osa-imessage": "2.4.2", | |
| "pino": "5.11.1" | |
| } | |
| } |
Author
Any information you can provide to replicate would be greatly appreciated!
The LLM potential is exactly what I’m interested in as well as maybe dynamic traffic notifications using home assistant to gauge work traffic for example and notify specific user via iMessage.
As for the additional component I mentioned, it was mostly due to the use of why they call private api to allow further capabilities like reacting to messages with a thumbs up for example. If your setup can mimic this then one less app I need and even better lol
Oh also what about in case of a group chat ? Can you do individual to either or and both? Thanks so much!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I will double check this gist to see if it matches my current setup. I have been using this setup (or similar) for years with great success.
To answer your questions, yes each threaded conversation is separate. So each person gets their own responses. In my current setup, I have the ability to control which commands can be sent from certain users.
I could add a service pretty easily that lives alongside the notify component. It looks like that blue bubble component does more or less what this does, so I'm not sure the need of connecting the two pieces of software since they do the same thing (more or less).
Lastly, I have not played around with utilizing LLM in combination with the conversation component, but it could pretty powerful.