Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changing to axios from request since request is being deprecated #38

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "Send and receive Slack Webhooks easily!",
"main": "slack.js",
"dependencies": {
"deferred": "0.7.1",
"request": "~2.x"
"deferred": "0.7.11",
"axios": "1.3.4"
},
"devDependencies": {},
"scripts": {
Expand All @@ -15,4 +15,4 @@
"author": "Ben Brown <[email protected]>",
"license": "MIT",
"readmeFilename": "readme.md"
}
}
52 changes: 32 additions & 20 deletions slack.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
"use strict";

var request = require('request');
var axios = require('axios');
var deferred = require('deferred');

function Slack(hook_url, http_proxy_options) {
this.hook_url = hook_url;
this.http_proxy_options = http_proxy_options;
}

Slack.prototype.send = function(message, cb) {
Slack.prototype.send = function (message, cb) {
if (!message.text) {
if (cb) cb.call(null,{message:'No text specified'},null);
if (cb) cb.call(null, { message: 'No text specified' }, null);
return;
}

var command = this.hook_url;
var body = {
text: message.text,
text: message.text,
};

if (message.username) { body.username = message.username; }
if (message.channel) { body.channel = message.channel; }
if (message.icon_url) { body.icon_url = message.icon_url; }
Expand All @@ -29,27 +29,39 @@ Slack.prototype.send = function(message, cb) {

var option = {
proxy: (this.http_proxy_options && this.http_proxy_options.proxy) || process.env.https_proxy || process.env.http_proxy,
url: command,
body: JSON.stringify(body)
url: command,
body: JSON.stringify(body)
};

if(!cb) var d = deferred();
if (!cb) var d = deferred();

var req = request.post(option, function(err, res, body) {
if (!err && body!='ok') {
err = {message: body};
body = null;
}
if (d) return err ? d.reject(err) : d.resolve({res: res, body: body});
if (cb) return cb.call(null, err, body);
return null;
});
let req = axios.post(option.url, option.body, {})
.then((response) => {
const res = response;
const body = response.data;
if (!body || body !== "ok") {
let err = { message: body };
throw err;
}
if (d) {
return d.resolve({ res, body });
} else if (cb) {
return cb.call(null, null, body);
}
})
.catch((error) => {
if (d) {
d.reject(error || { message: 'Axios Request error' });
} else if (cb) {
return cb.call(null, error || { message: 'Axios Request error' }, null);
}
});

return d ? d.promise : req;
};


Slack.prototype.respond = function(query,cb) {
Slack.prototype.respond = function (query, cb) {
var obj = {};

obj.token = query.token;
Expand All @@ -62,9 +74,9 @@ Slack.prototype.respond = function(query,cb) {
obj.text = query.text;

if (!cb) {
return {text:''};
return { text: '' };
} else {
return cb.call(null,obj);
return cb.call(null, obj);
}
};

Expand Down