Last active
June 28, 2022 13:55
-
-
Save rfletcher/2816fd69963760b21670ae0cc7e35d62 to your computer and use it in GitHub Desktop.
A simple bridge between iMessage and Home Assistant's conversation component
This file contains 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 ); | |
} ); |
This file contains 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: | |
- "[email protected]" | |
home_assistant: | |
host: "http://<your home assistant host or ip>" | |
port: 8123 | |
token: "<your home assistant token>" |
This file contains 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", | |
"homeassistant": "0.2.0", | |
"js-yaml": "3.12.1", | |
"osa-imessage": "2.4.2", | |
"pino": "5.11.1" | |
} | |
} |
Thank you for posting this! I've extended this to include a REST notify service that I recently integrated in my setup. Let me know if you want me to post it.
Edit: Forked here
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This represents an hour of two of tinkering, so use at your own risk, but here's a simple proof of concept bridge between Home Assistant and iMessage.
Requirements
Setup
npm install; node app.js
on that Mac.PS: The config file goes in
config/default.yaml
, but GitHub wouldn't let me include a slash in the name.