@zoomus/chatbot
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -11,2 +11,6 @@ ## 1.0.0 | ||
console error | ||
console error | ||
## 1.0.4 | ||
export request method |
{ | ||
"name": "@zoomus/chatbot", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Zoom Node.js Chatbot Library", | ||
@@ -61,7 +61,8 @@ "keywords": [ | ||
"abort-controller": "^3.0.0", | ||
"node-fetch": "^2.6.0", | ||
"date-fns": "^1.30.1", | ||
"debug": "^4.1.1", | ||
"delay": "^4.2.0" | ||
"delay": "^4.2.0", | ||
"form-data": "^3.0.0", | ||
"node-fetch": "^2.6.0" | ||
} | ||
} |
@@ -6,3 +6,3 @@ "use strict"; | ||
}); | ||
exports.log = exports.setting = exports.client = exports.oauth2 = undefined; | ||
exports.request = exports.log = exports.setting = exports.client = exports.oauth2 = undefined; | ||
@@ -25,6 +25,10 @@ var _index = require("./oauth2/index"); | ||
var _request = require("./utils/request"); | ||
var _request2 = _interopRequireDefault(_request); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
let oauth2 = function (appKey, appSecret, redirect_uri) { | ||
return new _index2.default(appKey, appSecret, redirect_uri); | ||
let oauth2 = function (appKey, appSecret, redirect_uri, baseUrl) { | ||
return new _index2.default(appKey, appSecret, redirect_uri, baseUrl); | ||
}; | ||
@@ -76,2 +80,3 @@ | ||
exports.setting = setting; | ||
exports.log = log; | ||
exports.log = log; | ||
exports.request = _request2.default; |
@@ -49,7 +49,8 @@ "use strict"; | ||
class Oauth2 extends _UserToken2.default { | ||
constructor(appKey, appSecret, redirect_uri) { | ||
constructor(appKey, appSecret, redirect_uri, baseUrl) { | ||
super(appKey, appSecret, redirect_uri); | ||
this.appKey = appKey; | ||
this.appSecret = appSecret; | ||
this.redirect_uri = redirect_uri; // this.store = {}; | ||
this.redirect_uri = redirect_uri; | ||
this.baseUrl = baseUrl; // this.store = {}; | ||
@@ -56,0 +57,0 @@ this.clientTokens = {}; |
@@ -109,7 +109,8 @@ "use strict"; | ||
redirect_uri, | ||
tokenCallback | ||
tokenCallback, | ||
baseUrl | ||
} = this; | ||
return new Promise((resolve, reject) => { | ||
// this.parseCode(code); | ||
(0, _oauth2.default)(appKey, appSecret, redirect_uri, code).then(async tokens => { | ||
(0, _oauth2.default)(appKey, appSecret, redirect_uri, code, baseUrl).then(async tokens => { | ||
let out = this.setTokens(tokens); | ||
@@ -116,0 +117,0 @@ |
@@ -21,3 +21,3 @@ "use strict"; | ||
let oauth2 = function (appKey, appSecret, redirect_uri, code) { | ||
let oauth2 = function (appKey, appSecret, redirect_uri, code, baseUrl) { | ||
// let { appKey, appSecret, redirect_uri, code: storeCode } = store.get(["appKey", "appSecret", "code", "redirect_uri"]); | ||
@@ -37,3 +37,4 @@ return new Promise((resolve, reject) => { | ||
code, | ||
redirect_uri | ||
redirect_uri, | ||
baseUrl | ||
}); | ||
@@ -40,0 +41,0 @@ |
@@ -38,5 +38,6 @@ "use strict"; | ||
redirect_uri, | ||
code | ||
code, | ||
baseUrl | ||
} = option; | ||
let url = `${_config2.default.url}/oauth/token`; | ||
let url = baseUrl || `${_config2.default.url}/oauth/token`; | ||
return `${url}?grant_type=authorization_code&code=${code}&redirect_uri=${redirect_uri}`; | ||
@@ -43,0 +44,0 @@ } else if (type in this) { |
@@ -23,2 +23,8 @@ "use strict"; | ||
var _formData = require("form-data"); | ||
var _formData2 = _interopRequireDefault(_formData); | ||
var _url = require("url"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -50,2 +56,3 @@ | ||
body, | ||
bodyType, | ||
method = 'get', | ||
@@ -58,4 +65,16 @@ url, | ||
return new Promise((resolve, reject) => { | ||
if (typeof body === 'object') { | ||
body = JSON.stringify(body); | ||
let newBody = body; | ||
if (bodyType === 'formParameters') { | ||
newBody = new _url.URLSearchParams(); | ||
Object.keys(body).forEach(key => { | ||
newBody.append(key, body[key]); | ||
}); | ||
} else if (bodyType === 'formData') { | ||
newBody = new _formData2.default(); | ||
Object.keys(body).forEach(key => { | ||
newBody.append(key, body[key]); | ||
}); | ||
} else if (typeof body === 'object') { | ||
newBody = JSON.stringify(body); | ||
} | ||
@@ -66,3 +85,3 @@ | ||
headers, | ||
body | ||
body: newBody | ||
}; | ||
@@ -92,2 +111,3 @@ let timeoutResult = null; | ||
let status = res.status; | ||
let headers = res.headers; | ||
res.text().then(responseBody => { | ||
@@ -100,9 +120,12 @@ let responseText = responseBody; | ||
} catch (e) { | ||
errorHandle(logDataOption, { | ||
type: 'parseError', | ||
status, | ||
message: responseText | ||
}, reject); | ||
return; | ||
} | ||
responseBody = responseText; //can't parse | ||
} // catch(e){ | ||
// errorHandle(logDataOption,{ | ||
// type:'parseError', | ||
// status, | ||
// message:responseText | ||
// },reject); | ||
// return; | ||
// } | ||
} | ||
@@ -126,3 +149,4 @@ | ||
status, | ||
body: responseBody | ||
body: responseBody, | ||
headers | ||
}); | ||
@@ -140,3 +164,3 @@ } else { | ||
errorHandle(logDataOption, { | ||
type: 'parseError', | ||
// type:'parseError', | ||
status, | ||
@@ -143,0 +167,0 @@ message: e |
@@ -11,3 +11,3 @@ # Zoom Node.js Chatbot Library | ||
$ npm install @zoomus/chatbot --save | ||
const { oauth2, client, setting, log } = require('@zoomus/chatbot'); | ||
const { oauth2, client, setting, log,request } = require('@zoomus/chatbot'); | ||
``` | ||
@@ -17,5 +17,7 @@ | ||
we have two type log of info,one is {type:'http',{error,request,response}},another is {type:'error_notice',message:{error}} this error include http error/webhook data error. | ||
you can use request method to auto log http information in custom logic,request({url:'',headers,body,bodyType,method});After request happen,will auto log information in the callback | ||
```js | ||
const { oauth2, client, setting, log } = require('@zoomus/chatbot'); | ||
//we have two type log of info,one is {type:'http',{error,request,response}},another is {type:'error_notice',message:{error}} this error include http error/webhook data error. | ||
const { oauth2, client, setting, log,request } = require('@zoomus/chatbot'); | ||
log(function(info) { | ||
@@ -30,4 +32,51 @@ let { type, message } = info; | ||
### Common request method | ||
Request is the method which wrap [node-fetch](https://www.npmjs.com/package/node-fetch) and put form-data and form-parameters in simple object | ||
```js | ||
const {request } = require('@zoomus/chatbot'); | ||
//application/json type | ||
request({ | ||
url:string, | ||
method:'post', | ||
headers:{}, | ||
body:{a:1,b:2} | ||
}); | ||
//form x-www-form-urlencoded | ||
request({ | ||
url:string, | ||
method:'post', | ||
headers:{}, | ||
body:{a:1,b:2}, | ||
bodyType:'formParameters' | ||
}); | ||
//form-data | ||
request({ | ||
url:string, | ||
method:'post', | ||
headers:{}, | ||
body:{a:1,b:2}, | ||
bodyType:'formData' | ||
}); | ||
//get query | ||
request({ | ||
url:string, | ||
method:'get', | ||
headers:{}, | ||
query:{ | ||
a:1,b:2 | ||
} | ||
}); | ||
``` | ||
### SendMessage Chatbot Message | ||
follow the code to send message to ZOOM IM chat | ||
```js | ||
@@ -54,4 +103,6 @@ const { oauth2, client, setting, log } = require('@zoomus/chatbot'); | ||
### Get ZOOM IM channel message | ||
### Get ZOOM IM chat message | ||
handle ZOOM IM chat webhook message,message have two sources, one is 'channel',another is 'bot'.And message have two types,one is 'slash',another is 'action' | ||
```js | ||
@@ -79,2 +130,4 @@ const { oauth2, client, setting, log } = require('@zoomus/chatbot'); | ||
use zoom oauth2 to request zoom openapi simple | ||
```js | ||
@@ -126,5 +179,5 @@ const { oauth2, client, setting, log } = require('@zoomus/chatbot'); | ||
zoomApp.auth.setTokens({//get tokens from database and set into zoomApp | ||
access_token: database.get('zoom_access_token'), | ||
refresh_token: database.get('zoom_refresh_token'), | ||
expires_date: database.get('zoom_access_token_expire_time') | ||
access_token: database.get('access_token'), | ||
refresh_token: database.get('refresh_token'), | ||
expires_date: database.get('expires_date') | ||
}); | ||
@@ -131,0 +184,0 @@ zoomApp.auth.callbackRefreshTokens((tokens,error) => {// when request v2/users/me fail by accesstoken expired,library will auto use refresh_token for request access_token. After that, this function will be called,you can save new access_token in database. and then will auto call request /v2/users/me again |
@@ -5,5 +5,6 @@ import Oauth2 from './oauth2/index'; | ||
import serviceLog from './services/log'; | ||
import request from './utils/request'; | ||
let oauth2 = function (appKey, appSecret, redirect_uri){ | ||
return new Oauth2(appKey, appSecret, redirect_uri); | ||
let oauth2 = function (appKey, appSecret, redirect_uri,baseUrl){ | ||
return new Oauth2(appKey, appSecret, redirect_uri,baseUrl); | ||
}; | ||
@@ -49,1 +50,2 @@ | ||
export {log}; | ||
export {request}; |
@@ -36,3 +36,3 @@ import utils from '../utils/index'; | ||
class Oauth2 extends UserToken { | ||
constructor(appKey, appSecret, redirect_uri) { | ||
constructor(appKey, appSecret, redirect_uri,baseUrl) { | ||
super(appKey, appSecret, redirect_uri); | ||
@@ -42,2 +42,3 @@ this.appKey = appKey; | ||
this.redirect_uri = redirect_uri; | ||
this.baseUrl=baseUrl; | ||
// this.store = {}; | ||
@@ -44,0 +45,0 @@ this.clientTokens={}; |
@@ -72,6 +72,6 @@ | ||
// } | ||
let { appKey, appSecret, redirect_uri,tokenCallback } = this; | ||
let { appKey, appSecret, redirect_uri,tokenCallback,baseUrl } = this; | ||
return new Promise((resolve, reject) => { | ||
// this.parseCode(code); | ||
oauth2(appKey, appSecret, redirect_uri, code) | ||
oauth2(appKey, appSecret, redirect_uri, code,baseUrl) | ||
.then(async (tokens) => { | ||
@@ -78,0 +78,0 @@ let out = this.setTokens(tokens); |
@@ -8,3 +8,3 @@ import utils from '../utils/index'; | ||
let oauth2 = function(appKey, appSecret, redirect_uri, code) { | ||
let oauth2 = function(appKey, appSecret, redirect_uri, code,baseUrl) { | ||
// let { appKey, appSecret, redirect_uri, code: storeCode } = store.get(["appKey", "appSecret", "code", "redirect_uri"]); | ||
@@ -23,3 +23,3 @@ | ||
let appString = createAppString(appKey, appSecret); | ||
let url = api.oauth2.get('url', { code, redirect_uri }); | ||
let url = api.oauth2.get('url', { code, redirect_uri,baseUrl }); | ||
let method = api.oauth2.get('method'); | ||
@@ -26,0 +26,0 @@ |
@@ -21,4 +21,4 @@ import config from './config'; | ||
if(type==='url'){ | ||
let { redirect_uri, code } = option; | ||
let url = `${config.url}/oauth/token`; | ||
let { redirect_uri, code,baseUrl } = option; | ||
let url =baseUrl||`${config.url}/oauth/token`; | ||
return `${url}?grant_type=authorization_code&code=${code}&redirect_uri=${redirect_uri}`; | ||
@@ -25,0 +25,0 @@ } |
@@ -6,2 +6,4 @@ import nodefetch from 'node-fetch'; | ||
import AbortController from 'abort-controller'; | ||
import FormData from 'form-data'; | ||
import {URLSearchParams} from 'url'; | ||
@@ -29,2 +31,3 @@ let errorHandle=(logDataOption,errorInfo,reject)=>{ | ||
body, | ||
bodyType, | ||
method = 'get', | ||
@@ -38,11 +41,23 @@ url, | ||
if(typeof body==='object'){ | ||
body=JSON.stringify(body); | ||
let newBody=body; | ||
if(bodyType==='formParameters'){ | ||
newBody = new URLSearchParams(); | ||
Object.keys(body).forEach((key)=>{ | ||
newBody.append(key,body[key]); | ||
}); | ||
} | ||
else if(bodyType==='formData'){ | ||
newBody = new FormData(); | ||
Object.keys(body).forEach((key)=>{ | ||
newBody.append(key,body[key]); | ||
}); | ||
} | ||
else if(typeof body==='object'){ | ||
newBody=JSON.stringify(body); | ||
} | ||
let dataOption={ | ||
method, | ||
headers, | ||
body | ||
body:newBody | ||
}; | ||
@@ -73,2 +88,3 @@ let timeoutResult=null; | ||
let status=res.status; | ||
let headers=res.headers; | ||
res.text().then((responseBody)=>{ | ||
@@ -81,9 +97,12 @@ let responseText=responseBody; | ||
catch(e){ | ||
errorHandle(logDataOption,{ | ||
type:'parseError', | ||
status, | ||
message:responseText | ||
},reject); | ||
return; | ||
responseBody=responseText;//can't parse | ||
} | ||
// catch(e){ | ||
// errorHandle(logDataOption,{ | ||
// type:'parseError', | ||
// status, | ||
// message:responseText | ||
// },reject); | ||
// return; | ||
// } | ||
} | ||
@@ -102,3 +121,3 @@ if (status >= 200 && status < 300){//success | ||
}); | ||
resolve({status,body:responseBody}); | ||
resolve({status,body:responseBody,headers}); | ||
} else {//success but status fail | ||
@@ -114,3 +133,3 @@ errorHandle(logDataOption,{ | ||
errorHandle(logDataOption,{ | ||
type:'parseError', | ||
// type:'parseError', | ||
status, | ||
@@ -117,0 +136,0 @@ message:e |
157230
5020
237
6
+ Addedform-data@^3.0.0
+ Addedasynckit@0.4.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedform-data@3.0.2(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)