Skip to content
This repository has been archived by the owner on Jul 2, 2022. It is now read-only.

Stopped working: RequestError #10

Closed
AlfaJackal opened this issue Jan 21, 2020 · 7 comments
Closed

Stopped working: RequestError #10

AlfaJackal opened this issue Jan 21, 2020 · 7 comments

Comments

@AlfaJackal
Copy link

Hi @oznu,

I really love your work, especially this one and docker-homebridge.

I just recognised that this wonderful event handler unexpectedly has stopped working. The only thing I am aware of is a apt-get update && apt-get upgrade.

I would really appreciate your help! Node is v13.3.0.

pi@xxx:~/node_modules/unifi-events $ node telegram-notification.js
RequestError: TypeError [ERR_UNESCAPED_CHARACTERS]: Request path contains unescaped characters
    at new RequestError (/home/pi/node_modules/request-promise-core/lib/errors.js:14:15)
    at Request.plumbing.callback (/home/pi/node_modules/request-promise-core/lib/plumbing.js:87:29)
    at Request.RP$callback [as _callback] (/home/pi/node_modules/request-promise-core/lib/plumbing.js:46:31)
    at self.callback (/home/pi/node_modules/request/request.js:185:22)
    at Request.emit (events.js:219:5)
    at Request.start (/home/pi/node_modules/request/request.js:753:10)
    at Request.write (/home/pi/node_modules/request/request.js:1497:10)
    at end (/home/pi/node_modules/request/request.js:549:18)
    at Immediate._onImmediate (/home/pi/node_modules/request/request.js:578:7)
    at processImmediate (internal/timers.js:444:21) {
  name: 'RequestError',
  message: 'TypeError [ERR_UNESCAPED_CHARACTERS]: Request path contains unescaped characters',
  cause: TypeError [ERR_UNESCAPED_CHARACTERS]: Request path contains unescaped characters
      at new ClientRequest (_http_client.js:147:13)
      at Object.request (https.js:308:10)
      at Request.start (/home/pi/node_modules/request/request.js:751:32)
      at Request.write (/home/pi/node_modules/request/request.js:1497:10)
      at end (/home/pi/node_modules/request/request.js:549:18)
      at Immediate._onImmediate (/home/pi/node_modules/request/request.js:578:7)
      at processImmediate (internal/timers.js:444:21) {
    code: 'ERR_UNESCAPED_CHARACTERS'
  },
  error: TypeError [ERR_UNESCAPED_CHARACTERS]: Request path contains unescaped characters
      at new ClientRequest (_http_client.js:147:13)
      at Object.request (https.js:308:10)
      at Request.start (/home/pi/node_modules/request/request.js:751:32)
      at Request.write (/home/pi/node_modules/request/request.js:1497:10)
      at end (/home/pi/node_modules/request/request.js:549:18)
      at Immediate._onImmediate (/home/pi/node_modules/request/request.js:578:7)
      at processImmediate (internal/timers.js:444:21) {
    code: 'ERR_UNESCAPED_CHARACTERS'
  },
  options: {
    json: {
      text: '✅Connected to <b>xxx</b>!%0A%0A<b>Device</b>: xxx%0A<b>AP:</b> AP Pro xx%0A<b>Channel:</b> 11',
      username: 'UniFi Notify'
    },
    uri: 'https://api.telegram.org/xxx/sendMessage?chat_id=xxx&parse_mode=html&text=✅Connected to <b>xxx</b>!%0A%0A<b>Device</b>: xxx%0A<b>AP:</b> AP Pro xx%0A<b>Channel:</b> 11',
    method: 'POST',
    callback: [Function: RP$callback],
    transform: undefined,
    simple: true,
    resolveWithFullResponse: false,
    transform2xxOnly: false
  },
  response: undefined
}

This is my .js:

const rp = require('request-promise')
const UnifiEvents = require('unifi-events')

// Enter the webhook url provided by Slack here
let webhook = 'https://api.telegram.org/xxx/sendMessage?chat_id=xxx&parse_mode=html&text='

let sendNotification = (text) => {
  return rp.post(webhook + text, {
    json: {
      text: text,
      username: 'UniFi Notify'
    }
  })
  .then(() => {
    console.log(text)
  })
  .catch((err) => {
    console.log(err)
  })
}

let unifi = new UnifiEvents({
  controller: 'https://10.0.0.2:8443',  // Required. The url of the UniFi Controller
  username: 'xxx',               // Required.
  password: 'xxx',                 // Required.
  site: 'default',                      // Optional. The UniFi site to connect to, if not set will use the default site.
  rejectUnauthorized: false,             // Optional. Set to false if you don't have a valid SSL
  listen: true                          // Optional. Set to false if you don't want to listen for events
})

unifi.on('ready', () => {
  console.log('Connected To UniFi Controller')
})

// Listen for users and guests connecting to the network
//unifi.on('connected', (data) => {
//  console.log(data)
//})

// Listen for users and guests disconnecting from the network
//unifi.on('disconnected', (data) => {
//  console.log(data)
//})

unifi.on('EVT_WG_Connected', (data) => {
  unifi.getAp(data.ap).then((ap) => {
    sendNotification(`✅Connected to <b>${data.ssid}</b>!%0A%0A<b>Device</b>: ${data.hostname}%0A<b>AP:</b> ${ap.name}%0A<b>Channel:</b> ${data.channel}`)
  })
})

unifi.on('EVT_WG_Disconnected', (data) => {
  unifi.getAp(data.ap).then((ap) => {
    sendNotification(`🚫<b>${data.hostname}</b> disconnected from <b>${data.ssid}</b> (${ap.name})`)
  })
})

// Listen for any event
//unifi.on('event', (data) => {
//  console.log(data)
//})

@AlfaJackal
Copy link
Author

Nothing? No problems at all or any help anyone?

@AlfaJackal
Copy link
Author

Fixed it by myself.

@coricidin12
Copy link

How did you fix this? I can't get it to connect to my controller at all, ip of "http://192.168.xx.xx:8443". Does the controller need to have an SSL cert for this library to work?

It never fires the 'Connected To UniFi Controller' event and if when I issue a getDevices(), I get an error that says "error: self-signed certificate"

@AlfaJackal
Copy link
Author

Have you set rejectUnauthorized: false?
This is for connecting to a controller without valid SSL cert.

@coricidin12
Copy link

yes, still doesn't work. Doesn't fire the connected event and when I issue getClients(), I get "bad request, this combination of host and port requires TLS".

This is my code:

`const UnifiEvents = require('unifi-events');

let unifi = new UnifiEvents({
controller: http://192.168.X.XX:8443,
username: settings.user,
password: settings.password,
rejectUnauthorized: false
})

unifi.on('ready', () => {
console.log('Connected To UniFi Controller')
})

unifi.on('connected', (data) => {
unifi.getAp(data.ap).then((ap) => {
sendNotification(Device *${data.hostname}* has Connected to *${data.ssid}* (${ap.name}) on channel ${data.channel}.)
})
})

unifi.on('disconnected', (data) => {
unifi.getAp(data.ap).then((ap) => {
sendNotification(Device *${data.hostname}* has Disconnected from *${data.ssid}* (${ap.name}))
})
})
unifi.getClients()`

@AlfaJackal
Copy link
Author

You have configured HTTP instead of HTTPS.

Try controller: https://192.168.X.XX:8443,

@coricidin12
Copy link

the "ready" event still isn't firing, but I am getting connect and disconnect events now. Thanks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants