alexwhitman / node-pushbullet-api

PushBullet API module for Node.js
165 stars 36 forks source link

Is it possible to push into a channel ? #27

Closed Ugarz closed 6 years ago

Ugarz commented 6 years ago

Hello,

I'm using this lib' to push datas to specific persons for a while. Now we are a lot more and I decided to give a try to push into a channel. I tried with postman without trouble but I can't find in the readme how to push in a damn channel. Could anyone help me to find out ?

export default function sendNotification(query) {
  const slugifiedQuery = slugify(query)
  const channel_tag = 'myAwesomeTag'
  return new Promise((resolveSending, rejectSending) => {
    function send() {
      return new Promise((resolve, reject) => {
        const link = `http://aprettysite.com/${slugifiedQuery}.html`;
        pusher.devices((error, devicesResponse) => {
          const promises = [];

          // For each device
          _.each(devicesResponse.devices, (device) => {
            // If active
            if (device.active) {    
              // SendLink
              const sendLink = new Promise((resolve, reject) => {
                pusher.channelInfo(channel_tag,(error, response) => {
                  if(error) reject(null, null, "Can not find channel inforamtions");

                  console.log('\n CHANNEL INFO', response)

                  // Push a damn link with the reponse.
                  // I tried to replace response.iden by response.tag with no success
                  pusher.link(response.iden, 'Hello I want ' + query, link, function(error, response) {
                    console.log(error ? error: 'ok ' + response)
                    if (error) {
                      reject(null, null, "Can not send notification :(");
                    } else {
                      resolve(response);
                    }
                  });

                });
              });
              promises.push(sendLink);
            }
          });
          Promise.all(promises).then(resolve).catch(reject);
        });
      });
    }

    send()
      .then(resolveSending)
      .catch(rejectSending);
  });
}

Here is my error

Error: The param 'device_iden' has an invalid value.
    at PushBullet.handleError (pushbullet.js:126)

I know that pusher.link() is to push a link to someone, but I can't find for a channel.

pusher.link('u1qSJddxeKwOGuGW', 'GitHub', 'https://github.com/', function(error, response) {});

Thanks a lot :)

alexwhitman commented 6 years ago

If you're just using a string then it will get interpreted as a device_iden (unless it contains @ in which case it'll be treated as an email). If you want to push to a channel you have to be more explicit:

Give this a try:

pusher.link({ channel_tag: 'insert-tag-here' }, 'GitHub', 'https://github.com/', function(error, response) {}

You can see the logic at https://github.com/alexwhitman/node-pushbullet-api/blob/master/lib/internal/pushes.js#L129

Ugarz commented 6 years ago

Thank you for your response ! I finally managed to do it 👍 but I didn't found it in the readme, I'd like to contribute adding it to the documentation if you're okay :)

FYI here's my new code wich has been a bit reduced haha 😀

/**
 * pushNotification
 * 
 * @param {string} query 
 * @returns 
 */
function pushNotification(query) {
  const slugifiedQuery = slugify(query)
  const link = `http://aprettysite.com/${slugifiedQuery}.html`;
  return pusher.link({ channel_tag: process.env.REACT_APP_PUSH_CHANNEL }, 'Ask for something ' + query, link);
}

Thanks for you time and your help, this lib' rox !

alexwhitman commented 6 years ago

It's kind of mentioned via the last item on https://github.com/alexwhitman/node-pushbullet-api/blob/master/README.md#target-devices but it could be clearer.

Ugarz commented 6 years ago

Closing the issue as I managed to do what I want, thanks for your help !