Skip to content
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
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"name": "node-slack",
"version": "0.0.6",
"name": "node-slack-axios",
"version": "0.2.1",
"description": "Send and receive Slack Webhooks easily!",
"main": "slack.js",
"dependencies": {
"deferred": "0.7.1",
"request": "~2.x"
"deferred": "0.7.11",
"axios": "^1.7.4"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 0"
},
"repository": "git@github.com:xoxco/node-slack.git",
"author": "Ben Brown <ben@xoxco.com>",
"repository": "git@github.com:chennakrishna8/node-slack-axios.git",
"author": "chennakrishna sagili <chennakrishna.vision@gmail.com>",
"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