Skip to content

Instantly share code, notes, and snippets.

@jmathai
Forked from rfletcher/app.js
Created February 20, 2019 08:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmathai/e54dcbf3761cf642d04e6200c8a43dd9 to your computer and use it in GitHub Desktop.
Save jmathai/e54dcbf3761cf642d04e6200c8a43dd9 to your computer and use it in GitHub Desktop.
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