nimbleape / asterisk-dialogflow-rtp-audioserver

MIT License
41 stars 18 forks source link

How to use this library as... a library! #17

Open ghost opened 4 years ago

ghost commented 4 years ago

Since I discovered that I can simply subscribe to MQTT data in an external script, this makes https://github.com/nimbleape/asterisk-dialogflow-rtp-audioserver/issues/8 a bit redundant and means I don't need to fiddle with Dan's core code. I've also learnt a lot about Asterisk, JS and Dialogflow from digging through it, so here's how I've got it going, all in one place:

npm i -D github:nimbleape/asterisk-dialogflow-rtp-audioserver.git
npm i -D github:nimbleape/asterisk-dialogflow-ari-bridge.git 

Then merge the two config/default files into one at the same level (ie, above node_modules).

Create a launcher:

const rtpLib = require('asterisk-dialogflow-rtp-audioserver')
const ariLib = require('asterisk-dialogflow-ari-bridge')

Create a file where you want to process the result

const MQTT = require('async-mqtt')
const mqtt = MQTT.connect('mqtt://test.mosquitto.org', { connectTimeout: 250, reconnectPeriod: 0, keepalive: 0 })

const doStuff = async () => {
  console.log('Starting')
  try {
    await mqtt.subscribe('dialogflow-asterisk/#')
    console.log('Done')
  } catch (e) {
    console.log(e.stack)
    process.exit()
  }
}

mqtt.on('connect', doStuff)

mqtt.on('message', function (topic, messageBuffer) {
  const message = JSON.parse(messageBuffer.toString())
  if (message && message.intent && message.intent.parameters && message.intent.parameters.fields) {
    console.log(message.intent.parameters.fields) // do stuff here
  }
})

... and run them both with something like pm2:

It's very possible that everyone knew all this already, but previously I had 3 directories - 2 for Dan's two libraries and one where my Asterisk scripts ran. Now everything is all in one, with one node_modules directory, one package.json and so on.

If anyone is doing it a better way, let me know! PS - forget what it says in the README about needing MQTT. If you just use Dan's amazing scripts "as is", then it uses an external service (just be aware anyone can spy on it if you don't use your own, not that it matters in my case).

Really good scripts, Dan - thanks again!