Skip to content

Instantly share code, notes, and snippets.

@niemyjski
Forked from rfletcher/app.js
Created Feb 19, 2019
Embed
What would you like to do?
A simple bridge between iMessage and Home Assistant's conversation component
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 );
} );
---
allowed_senders:
- "me@example.com"
home_assistant:
host: "http://<your home assistant host or ip>"
port: 8123
token: "<your home assistant token>"
{
"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"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment