Skip to content

Commit

Permalink
some fix and add new method -> getAdmins
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Vershinin committed Oct 23, 2019
1 parent 064e565 commit 157d02d
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 46 deletions.
3 changes: 3 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"esversion": 6
}
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Node.js TamTam Bot API

[![Bot API](https://img.shields.io/badge/TamTam%20Bot%20API-0.1.6-blue.svg)](https://dev.tamtam.chat)
[![Bot API](https://img.shields.io/badge/TamTam%20Bot%20API-0.1.8-blue.svg)](https://dev.tamtam.chat)
[![Build Status](https://travis-ci.com/vershininivan/node-tamtam-botapi.svg?branch=master)](https://travis-ci.org/vershininivan/node-tamtam-botapi)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fvershininivan%2Fnode-tamtam-botapi.svg?type=shield)]()
[![codecov](https://codecov.io/gh/vershininivan/node-tamtam-botapi/branch/master/graph/badge.svg)](https://codecov.io/gh/vershininivan/node-tamtam-botapi)
Expand Down Expand Up @@ -52,13 +52,6 @@ app.post(`/${path}`, (req, res) => {
});
});

// GET method route
app.get('/webhook', function (req, res) {
res.send({
success: true
});
});

// Start Express Server
app.listen(PORT, () => {
console.log(`Express server is listening on ${PORT}`);
Expand Down
125 changes: 99 additions & 26 deletions core/tamtam.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const _methods = {
SEND_ACTION: 'sendAction',
GET_MEMERSHIP: 'getMembership',
LEAVE_CHAT: 'leaveChat',
GET_ADMINS: 'getAdmins',
GET_MEMBERS: 'getMembers',
ADD_MEMBERS: 'addMembers',
REMOVE_MEMBER: 'removeMember',
Expand Down Expand Up @@ -53,6 +54,10 @@ const _updateTypes = [
'chat_title_changed'
];

const _uploadTypes = [
'photo', 'video', 'audio', 'file'
];

class TamTamBot extends EventEmitter {

constructor(configs, options = {}) {
Expand Down Expand Up @@ -113,6 +118,10 @@ class TamTamBot extends EventEmitter {
builder.verbs = 'DELETE';
builder.url = `${this.options.baseApiUrl}/chats/${_chatId}/members/me`;
break;
case _methods.GET_ADMINS:
builder.verbs = 'GET';
builder.url = `${this.options.baseApiUrl}/chats/${_chatId}/members/admins`;
break;
case _methods.GET_MEMBERS:
builder.verbs = 'GET';
builder.url = `${this.options.baseApiUrl}/chats/${_chatId}/members`;
Expand Down Expand Up @@ -194,6 +203,7 @@ class TamTamBot extends EventEmitter {
qs.timeout = form.timeout;
qs.types = form.types;
qs.type = form.type;
qs.session_id = form.session_id;
qs.access_token = this.token;
qs.v = this.version;
return qs;
Expand Down Expand Up @@ -229,9 +239,12 @@ class TamTamBot extends EventEmitter {
* @param {Object} update
*/
webhookUpdateTypeHandler(update = {}) {
console.log(update, _updateTypes.includes(update.update_type));
if (update.update_type !== undefined) {
if (_updateTypes.includes(update.update_type)) {
this.emit(update.update_type, update);
} else {
throw new Error('Can not find parameter \'' + update.update_type + '\' in response body');
}
} else {
throw new Error('Can not find parameter \'update_type\' in response body');
Expand Down Expand Up @@ -267,7 +280,9 @@ class TamTamBot extends EventEmitter {
getMyInfo(form = {}) {
form.method = this._methodBuilder(_methods.GET_MY_INFO);
form.query = this._buildQuery(form);
return TamTamBot._request({form});
return TamTamBot._request({
form
});
}

/**
Expand All @@ -283,7 +298,9 @@ class TamTamBot extends EventEmitter {
form.body = body;
form.method = this._methodBuilder(_methods.EDIT_MY_INFO);
form.query = this._buildQuery(form);
return TamTamBot._request({form});
return TamTamBot._request({
form
});
}

/**
Expand All @@ -301,7 +318,9 @@ class TamTamBot extends EventEmitter {
form.marker = marker;
form.method = this._methodBuilder(_methods.GET_ALL_CHATS);
form.query = this._buildQuery(form);
return TamTamBot._request({form});
return TamTamBot._request({
form
});
}

/**
Expand All @@ -316,7 +335,9 @@ class TamTamBot extends EventEmitter {
getChat(chatId, form = {}) {
form.method = this._methodBuilder(_methods.GET_CHAT, chatId);
form.query = this._buildQuery(form);
return TamTamBot._request({form})
return TamTamBot._request({
form
});
}

/**
Expand All @@ -333,7 +354,9 @@ class TamTamBot extends EventEmitter {
form.body = body;
form.method = this._methodBuilder(_methods.EDIT_CHAT, chatId);
form.query = this._buildQuery(form);
return TamTamBot._request({form})
return TamTamBot._request({
form
});
}

/**
Expand All @@ -349,7 +372,9 @@ class TamTamBot extends EventEmitter {
form.body = body;
form.method = this._methodBuilder(_methods.SEND_ACTION, chatId);
form.query = this._buildQuery(form);
return TamTamBot._request({form})
return TamTamBot._request({
form
});
}

/**
Expand All @@ -364,7 +389,9 @@ class TamTamBot extends EventEmitter {
getMembership(chatId, form = {}) {
form.method = this._methodBuilder(_methods.GET_MEMERSHIP, chatId);
form.query = this._buildQuery(form);
return TamTamBot._request({form})
return TamTamBot._request({
form
});
}

/**
Expand All @@ -379,7 +406,24 @@ class TamTamBot extends EventEmitter {
leaveChat(chatId, form = {}) {
form.method = this._methodBuilder(_methods.LEAVE_CHAT, chatId);
form.query = this._buildQuery(form);
return TamTamBot._request({form})
return TamTamBot._request({
form
});
}

/**
* Get chat admins
* https://dev.tamtam.chat/#operation/getAdmins
* @param {Number} chatId
* @param form
* @returns {request.Request}
*/
getAdmins(chatId, form = {}) {
form.method = this._methodBuilder(_methods.GET_ADMINS, chatId);
form.query = this._buildQuery(form);
return TamTamBot._request({
form
});
}

/**
Expand All @@ -399,7 +443,9 @@ class TamTamBot extends EventEmitter {
form.count = count;
form.method = this._methodBuilder(_methods.GET_MEMBERS, chatId);
form.query = this._buildQuery(form);
return TamTamBot._request({form})
return TamTamBot._request({
form
});
}

/**
Expand All @@ -416,7 +462,9 @@ class TamTamBot extends EventEmitter {
form.body = body;
form.method = this._methodBuilder(_methods.ADD_MEMBERS, chatId);
form.query = this._buildQuery(form);
return TamTamBot._request({form})
return TamTamBot._request({
form
});
}

/**
Expand All @@ -433,7 +481,9 @@ class TamTamBot extends EventEmitter {
form.user_id = userId;
form.method = this._methodBuilder(_methods.REMOVE_MEMBER, chatId);
form.query = this._buildQuery(form);
return TamTamBot._request({form})
return TamTamBot._request({
form
});
}

/**
Expand All @@ -459,7 +509,9 @@ class TamTamBot extends EventEmitter {
form.count = count;
form.method = this._methodBuilder(_methods.GET_MESSAGES);
form.query = this._buildQuery(form);
return TamTamBot._request({form})
return TamTamBot._request({
form
});
}

/**
Expand All @@ -479,7 +531,9 @@ class TamTamBot extends EventEmitter {
form.body = body;
form.method = this._methodBuilder(_methods.SEND_MESSAGE, chatId);
form.query = this._buildQuery(form);
return TamTamBot._request({form});
return TamTamBot._request({
form
});
}

/**
Expand All @@ -499,7 +553,9 @@ class TamTamBot extends EventEmitter {
form.body = body;
form.method = this._methodBuilder(_methods.EDIT_MESSAGE);
form.query = this._buildQuery(form);
return TamTamBot._request({form})
return TamTamBot._request({
form
});
}

/**
Expand All @@ -515,7 +571,9 @@ class TamTamBot extends EventEmitter {
form.message_id = messageId;
form.method = this._methodBuilder(_methods.DELETE_MESSAGE);
form.query = this._buildQuery(form);
return TamTamBot._request({form})
return TamTamBot._request({
form
});
}

/**
Expand All @@ -534,7 +592,9 @@ class TamTamBot extends EventEmitter {
form.body = body;
form.method = this._methodBuilder(_methods.ANSWER_ON_CALLBACK);
form.query = this._buildQuery(form);
return TamTamBot._request({form})
return TamTamBot._request({
form
});
}

/**
Expand All @@ -548,7 +608,9 @@ class TamTamBot extends EventEmitter {
getSubscriptions(form = {}) {
form.method = this._methodBuilder(_methods.GET_SUBSCRIPTIONS);
form.query = this._buildQuery(form);
return TamTamBot._request({form})
return TamTamBot._request({
form
});
}

/**
Expand All @@ -565,7 +627,9 @@ class TamTamBot extends EventEmitter {
form.body = body;
form.query = this._buildQuery(form);
form.method = this._methodBuilder(_methods.SUBSCRIBE);
return TamTamBot._request({form})
return TamTamBot._request({
form
});
}

/**
Expand All @@ -582,7 +646,9 @@ class TamTamBot extends EventEmitter {
form.url = url;
form.query = this._buildQuery(form);
form.method = this._methodBuilder(_methods.UNSUBSCRIBE);
return TamTamBot._request({form})
return TamTamBot._request({
form
});
}

/**
Expand All @@ -604,7 +670,9 @@ class TamTamBot extends EventEmitter {
form.types = types;
form.query = this._buildQuery(form);
form.method = this._methodBuilder(_methods.GET_UPDATES);
return TamTamBot._request({form})
return TamTamBot._request({
form
});
}

/**
Expand All @@ -616,12 +684,17 @@ class TamTamBot extends EventEmitter {
* @param form
*/
getUploadUrl(type, form = {}) {
form.type = type;
form.query = this._buildQuery(form);
form.method = this._methodBuilder(_methods.GET_UPLOAD_URL);
return TamTamBot._request({form})
if (type !== undefined || !_uploadTypes.includes(type)) {
form.type = type;
form.query = this._buildQuery(form);
form.method = this._methodBuilder(_methods.GET_UPLOAD_URL);
return TamTamBot._request({
form
});
} else {
throw new Error('Invalid parameter \`type\`. Should be one of: [photo,video,audio,file]');
}
}

}

module.exports = TamTamBot;
module.exports = TamTamBot;
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-tamtam-botapi",
"version": "0.2.0",
"version": "0.1.10",
"description": "TamTam Bot API",
"main": "./index.js",
"directories": {
Expand Down
Loading

0 comments on commit 157d02d

Please sign in to comment.