myteam-bot-sdk
Advanced tools
Comparing version 1.0.0-3 to 1.0.0-4
@@ -47,30 +47,3 @@ 'use strict'; | ||
***************************************************************************** */ | ||
/* global Reflect, Promise */ | ||
var extendStatics = function(d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
function __extends(d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
} | ||
var __assign = function() { | ||
__assign = Object.assign || function __assign(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
function __awaiter(thisArg, _arguments, P, generator) { | ||
@@ -86,39 +59,11 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
function __generator(thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
} | ||
var URLBuilder = /** @class */ (function () { | ||
function URLBuilder(url, baseUrl) { | ||
class URLBuilder { | ||
constructor(url, baseUrl) { | ||
this._url = new URL(url, baseUrl); | ||
} | ||
URLBuilder.prototype.appendQuery = function (name, value) { | ||
appendQuery(name, value) { | ||
this._url.searchParams.append(name, String(value)); | ||
return this; | ||
}; | ||
URLBuilder.prototype.appendQueryIfTruthy = function (name, value) { | ||
} | ||
appendQueryIfTruthy(name, value) { | ||
if (!value) { | ||
@@ -128,16 +73,14 @@ return this; | ||
return this.appendQuery(name, value); | ||
}; | ||
URLBuilder.prototype.appendQueryObject = function (value) { | ||
var keys = Object.keys(value); | ||
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) { | ||
var key = keys_1[_i]; | ||
} | ||
appendQueryObject(value) { | ||
const keys = Object.keys(value); | ||
for (const key of keys) { | ||
this.appendQuery(key, value[key]); | ||
} | ||
return this; | ||
}; | ||
URLBuilder.prototype.toString = function () { | ||
} | ||
toString() { | ||
return this._url.toString(); | ||
}; | ||
return URLBuilder; | ||
}()); | ||
} | ||
} | ||
@@ -198,4 +141,4 @@ function isMyTeamStickerPart(part) { | ||
} | ||
var MessageBuilder = /** @class */ (function () { | ||
function MessageBuilder() { | ||
class MessageBuilder { | ||
constructor() { | ||
this.textValue = ''; | ||
@@ -205,7 +148,7 @@ this.replyMsgId = []; | ||
} | ||
MessageBuilder.prototype.text = function (value) { | ||
text(value) { | ||
this.textValue = value; | ||
return this; | ||
}; | ||
MessageBuilder.prototype.replyTo = function (msgId) { | ||
} | ||
replyTo(msgId) { | ||
this.replyMsgId.push(msgId); | ||
@@ -215,4 +158,4 @@ this.forwardChatId = undefined; | ||
return this; | ||
}; | ||
MessageBuilder.prototype.forwardFrom = function (msgId, chatId) { | ||
} | ||
forwardFrom(msgId, chatId) { | ||
this.replyMsgId = []; | ||
@@ -225,4 +168,4 @@ if (this.forwardChatId !== chatId) { | ||
return this; | ||
}; | ||
MessageBuilder.prototype.buttonRow = function () { | ||
} | ||
buttonRow() { | ||
if (!this.inlineKeyboardMarkup) { | ||
@@ -235,12 +178,12 @@ this.inlineKeyboardMarkup = [[]]; | ||
return this; | ||
}; | ||
MessageBuilder.prototype.button = function (button) { | ||
} | ||
button(button) { | ||
if (!this.inlineKeyboardMarkup) { | ||
this.buttonRow(); | ||
} | ||
var row = this.inlineKeyboardMarkup[this.inlineKeyboardMarkup.length - 1]; | ||
const row = this.inlineKeyboardMarkup[this.inlineKeyboardMarkup.length - 1]; | ||
row.push(button); | ||
return this; | ||
}; | ||
MessageBuilder.prototype.formatText = function (type, value) { | ||
} | ||
formatText(type, value) { | ||
if (type === 'none') { | ||
@@ -253,11 +196,11 @@ this.textValue += value; | ||
} | ||
var offset = this.textValue.length; | ||
var length = value.length; | ||
const offset = this.textValue.length; | ||
const length = value.length; | ||
this.textValue += value; | ||
return this.formatRange(type, { | ||
offset: offset, | ||
length: length, | ||
offset, | ||
length, | ||
}); | ||
}; | ||
MessageBuilder.prototype.formatRange = function (type, value) { | ||
} | ||
formatRange(type, value) { | ||
var _a, _b; | ||
@@ -269,4 +212,4 @@ this.parseMode = undefined; | ||
return this; | ||
}; | ||
MessageBuilder.prototype.markdown = function (value) { | ||
} | ||
markdown(value) { | ||
this.format = undefined; | ||
@@ -276,4 +219,4 @@ this.parseMode = 'MarkdownV2'; | ||
return this; | ||
}; | ||
MessageBuilder.prototype.html = function (value) { | ||
} | ||
html(value) { | ||
this.format = undefined; | ||
@@ -283,5 +226,5 @@ this.parseMode = 'HTML'; | ||
return this; | ||
}; | ||
MessageBuilder.prototype.toObject = function () { | ||
var result = { | ||
} | ||
toObject() { | ||
const result = { | ||
text: this.textValue, | ||
@@ -306,10 +249,7 @@ }; | ||
return result; | ||
}; | ||
return MessageBuilder; | ||
}()); | ||
} | ||
} | ||
var CallbackQueryAnswerBuilder = /** @class */ (function () { | ||
function CallbackQueryAnswerBuilder() { | ||
} | ||
CallbackQueryAnswerBuilder.prototype.text = function (value) { | ||
class CallbackQueryAnswerBuilder { | ||
text(value) { | ||
this.textValue = value; | ||
@@ -319,4 +259,4 @@ this.showAlert = undefined; | ||
return this; | ||
}; | ||
CallbackQueryAnswerBuilder.prototype.alert = function (text) { | ||
} | ||
alert(text) { | ||
this.textValue = text; | ||
@@ -326,4 +266,4 @@ this.showAlert = true; | ||
return this; | ||
}; | ||
CallbackQueryAnswerBuilder.prototype.url = function (value) { | ||
} | ||
url(value) { | ||
this.textValue = undefined; | ||
@@ -333,5 +273,5 @@ this.showAlert = undefined; | ||
return this; | ||
}; | ||
CallbackQueryAnswerBuilder.prototype.toObject = function () { | ||
var result = {}; | ||
} | ||
toObject() { | ||
const result = {}; | ||
if (isString(this.urlValue)) { | ||
@@ -347,7 +287,6 @@ result.url = this.urlValue; | ||
return result; | ||
}; | ||
return CallbackQueryAnswerBuilder; | ||
}()); | ||
} | ||
} | ||
var R_COMMAND = /^\/\w+/gu; | ||
const R_COMMAND = /^\/\w+/gu; | ||
function readCommand(source) { | ||
@@ -357,3 +296,3 @@ if (typeof source !== 'string') { | ||
} | ||
var matchResult = source.match(R_COMMAND); | ||
const matchResult = source.match(R_COMMAND); | ||
if (matchResult == null) { | ||
@@ -364,13 +303,10 @@ return undefined; | ||
} | ||
var MyTeamSDKError = /** @class */ (function (_super) { | ||
__extends(MyTeamSDKError, _super); | ||
function MyTeamSDKError(description, serverResponse) { | ||
var _this = _super.call(this, (description !== null && description !== void 0 ? description : 'Unknown MyTeam API Error') + ' : ' + JSON.stringify(serverResponse)) || this; | ||
_this.serverResponse = serverResponse; | ||
return _this; | ||
class MyTeamSDKError extends Error { | ||
constructor(description, serverResponse) { | ||
super((description !== null && description !== void 0 ? description : 'Unknown MyTeam API Error') + ' : ' + JSON.stringify(serverResponse)); | ||
this.serverResponse = serverResponse; | ||
} | ||
return MyTeamSDKError; | ||
}(Error)); | ||
var MyTeamCallbackQueryEventEx = /** @class */ (function () { | ||
function MyTeamCallbackQueryEventEx(_sdk, eventId, payload) { | ||
} | ||
class MyTeamCallbackQueryEventEx { | ||
constructor(_sdk, eventId, payload) { | ||
this._sdk = _sdk; | ||
@@ -382,18 +318,15 @@ this.eventId = eventId; | ||
} | ||
MyTeamCallbackQueryEventEx.prototype.answer = function () { | ||
answer() { | ||
return this._sdk.answerCallbackQuery(this.payload.queryId, this.replyData); | ||
}; | ||
return MyTeamCallbackQueryEventEx; | ||
}()); | ||
var MyTeamSDK = /** @class */ (function (_super) { | ||
__extends(MyTeamSDK, _super); | ||
function MyTeamSDK(options) { | ||
var _this = _super.call(this) || this; | ||
_this._running = false; | ||
_this._lastEventId = 0; | ||
_this._handleSDKResponse = function (result) { | ||
} | ||
} | ||
class MyTeamSDK extends EventEmitter__namespace { | ||
constructor(options) { | ||
super(); | ||
this._running = false; | ||
this._lastEventId = 0; | ||
this._handleSDKResponse = (result) => { | ||
return Promise | ||
.all([Promise.resolve(result), result.json()]) | ||
.then(function (_a) { | ||
var result = _a[0], json = _a[1]; | ||
.then(([result, json]) => { | ||
if (json.ok) { | ||
@@ -408,3 +341,3 @@ return json; | ||
}; | ||
var defaultOptions = { | ||
const defaultOptions = { | ||
baseURL: 'https://myteam.mail.ru/bot/v1/', | ||
@@ -414,26 +347,24 @@ token: options.token, | ||
}; | ||
_this._options = __assign(__assign({}, defaultOptions), options); | ||
return _this; | ||
this._options = Object.assign(Object.assign({}, defaultOptions), options); | ||
} | ||
MyTeamSDK.prototype.listen = function () { | ||
listen() { | ||
this._running = true; | ||
this._mainLoop(); | ||
return this; | ||
}; | ||
MyTeamSDK.prototype.stop = function () { | ||
} | ||
stop() { | ||
this._running = false; | ||
return this; | ||
}; | ||
MyTeamSDK.prototype.addCommand = function (aliases, handler) { | ||
} | ||
addCommand(aliases, handler) { | ||
aliases = Array.isArray(aliases) ? aliases : [aliases]; | ||
for (var _i = 0, aliases_1 = aliases; _i < aliases_1.length; _i++) { | ||
var alias = aliases_1[_i]; | ||
this.on("command:" + alias, handler); | ||
for (const alias of aliases) { | ||
this.on(`command:${alias}`, handler); | ||
} | ||
}; | ||
MyTeamSDK.prototype.on = function (event, handler) { | ||
return _super.prototype.on.call(this, event, handler); | ||
}; | ||
MyTeamSDK.prototype.sendText = function (chatId, text) { | ||
var message = text instanceof MessageBuilder | ||
} | ||
on(event, handler) { | ||
return super.on(event, handler); | ||
} | ||
sendText(chatId, text) { | ||
const message = text instanceof MessageBuilder | ||
? text | ||
@@ -444,3 +375,3 @@ : new MessageBuilder().text(text); | ||
.appendQueryObject(message.toObject()) | ||
.toString()).then(function (json) { | ||
.toString()).then((json) => { | ||
if (typeof json.msgId !== 'string') { | ||
@@ -453,5 +384,5 @@ throw new MyTeamSDKError('Bad sendText response', { | ||
}); | ||
}; | ||
MyTeamSDK.prototype.editText = function (chatId, msgId, text) { | ||
var message = text instanceof MessageBuilder | ||
} | ||
editText(chatId, msgId, text) { | ||
const message = text instanceof MessageBuilder | ||
? text | ||
@@ -464,4 +395,4 @@ : new MessageBuilder().text(text); | ||
.toString()); | ||
}; | ||
MyTeamSDK.prototype.getMembers = function (chatId, query, cursor) { | ||
} | ||
getMembers(chatId, query, cursor) { | ||
return this.get(new URLBuilder('chats/getMembers', this._options.baseURL) | ||
@@ -471,3 +402,3 @@ .appendQuery('chatId', chatId) | ||
.appendQueryIfTruthy('cursor', cursor) | ||
.toString()).then(function (json) { | ||
.toString()).then((json) => { | ||
if (Array.isArray(json.members)) { | ||
@@ -478,5 +409,5 @@ return json.members; | ||
}); | ||
}; | ||
MyTeamSDK.prototype.answerCallbackQuery = function (queryId, text) { | ||
var answer = text instanceof CallbackQueryAnswerBuilder | ||
} | ||
answerCallbackQuery(queryId, text) { | ||
const answer = text instanceof CallbackQueryAnswerBuilder | ||
? text | ||
@@ -488,4 +419,4 @@ : (text ? new CallbackQueryAnswerBuilder().text(text) : new CallbackQueryAnswerBuilder()); | ||
.toString()); | ||
}; | ||
MyTeamSDK.prototype.get = function (url) { | ||
} | ||
get(url) { | ||
if (typeof url === 'string') { | ||
@@ -497,4 +428,4 @@ url = new URLBuilder(url, this._options.baseURL); | ||
.then(this._handleSDKResponse); | ||
}; | ||
MyTeamSDK.prototype.post = function (url, body) { | ||
} | ||
post(url, body) { | ||
if (typeof url === 'string') { | ||
@@ -504,78 +435,56 @@ url = new URLBuilder(url, this._options.baseURL); | ||
url.appendQuery('token', this._options.token); | ||
return fetch__default['default'](url.toString(), { method: 'POST', body: body }) | ||
return fetch__default['default'](url.toString(), { method: 'POST', body }) | ||
.then(this._handleSDKResponse); | ||
}; | ||
MyTeamSDK.prototype._handleCommand = function (event) { | ||
var command = readCommand(event.payload.text); | ||
} | ||
_handleCommand(event) { | ||
const command = readCommand(event.payload.text); | ||
if (typeof command !== 'string' || !command.length) { | ||
return; | ||
} | ||
var args = event.payload.text.slice(command.length).trim(); | ||
var context = { | ||
const args = event.payload.text.slice(command.length).trim(); | ||
const context = { | ||
sdk: this, | ||
command: command, | ||
args: args, | ||
event: event, | ||
command, | ||
args, | ||
event, | ||
}; | ||
this.emit("command:" + command, context); | ||
}; | ||
MyTeamSDK.prototype._mainLoop = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var error_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!this._running) return [3 /*break*/, 5]; | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, this._mainLoopIter()]; | ||
case 2: | ||
_a.sent(); | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
error_1 = _a.sent(); | ||
this.emit('error', error_1); | ||
return [3 /*break*/, 4]; | ||
case 4: return [3 /*break*/, 0]; | ||
case 5: return [2 /*return*/]; | ||
this.emit(`command:${command}`, context); | ||
} | ||
_mainLoop() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
while (this._running) { | ||
try { | ||
yield this._mainLoopIter(); | ||
} | ||
}); | ||
catch (error) { | ||
this.emit('error', error); | ||
} | ||
} | ||
}); | ||
}; | ||
MyTeamSDK.prototype._mainLoopIter = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var json, _i, _a, event_1; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: return [4 /*yield*/, this.get(new URLBuilder('events/get', this._options.baseURL) | ||
.appendQuery('lastEventId', this._lastEventId) | ||
.appendQuery('pollTime', this._options.pollTime) | ||
.toString())]; | ||
case 1: | ||
json = _b.sent(); | ||
// Эмитим события для всего, что пришло от сервера | ||
for (_i = 0, _a = json.events; _i < _a.length; _i++) { | ||
event_1 = _a[_i]; | ||
this._lastEventId = event_1.eventId; | ||
if (isMyTeamCallbackQueryEvent(event_1)) { | ||
event_1 = new MyTeamCallbackQueryEventEx(this, event_1.eventId, event_1.payload); | ||
} | ||
else if (isMyTeamNewMessageEvent(event_1) || isMyTeamEditedMessageEvent(event_1)) { | ||
this._handleCommand(event_1); | ||
} | ||
try { | ||
this.emit(event_1.type, event_1); | ||
} | ||
catch (error) { | ||
this.emit('error', error); | ||
} | ||
} | ||
return [2 /*return*/]; | ||
} | ||
_mainLoopIter() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const json = yield this.get(new URLBuilder('events/get', this._options.baseURL) | ||
.appendQuery('lastEventId', this._lastEventId) | ||
.appendQuery('pollTime', this._options.pollTime) | ||
.toString()); | ||
// Эмитим события для всего, что пришло от сервера | ||
for (let event of json.events) { | ||
this._lastEventId = event.eventId; | ||
if (isMyTeamCallbackQueryEvent(event)) { | ||
event = new MyTeamCallbackQueryEventEx(this, event.eventId, event.payload); | ||
} | ||
}); | ||
else if (isMyTeamNewMessageEvent(event) || isMyTeamEditedMessageEvent(event)) { | ||
this._handleCommand(event); | ||
} | ||
try { | ||
this.emit(event.type, event); | ||
} | ||
catch (error) { | ||
this.emit('error', error); | ||
} | ||
} | ||
}); | ||
}; | ||
return MyTeamSDK; | ||
}(EventEmitter__namespace)); | ||
} | ||
} | ||
@@ -582,0 +491,0 @@ exports.CallbackQueryAnswerBuilder = CallbackQueryAnswerBuilder; |
{ | ||
"name": "myteam-bot-sdk", | ||
"version": "1.0.0-3", | ||
"version": "1.0.0-4", | ||
"repository": "https://github.com/8coon/myteam-bot-sdk", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
26779
720