~/Projects/node-serverchan
git clone https://code.lsong.org/node-serverchan
Commit
- Commit
- c8090635a38f31bae11597a4973f1296d82b35d5
- Author
- Lsong <[email protected]>
- Date
- 2021-10-06 20:38:17 +0800 +0800
- Diffstat
README.md | 20 ++++++++++++++++++++ example/index.js | 10 ++++++++++ package.json | 25 +++++++++++++++++++++++++ serverchan.js | 45 +++++++++++++++++++++++++++++++++++++++++++++
update
diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..fb764dbc4f5e4c4b290882eee2096668c050babb --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +## ServerChan + +> ServerChan API for Node.js + +https://sct.ftqq.com + +```js +const ServerChan = require('serverchan'); + +const chan = new ServerChan({ + sckey: 'SCT82268Td2xus2Ms6YDvKbVIqaNNELzx' +}); + +(async () => { + const res = await chan.sendMessage('node-serverchan', 'hello world') + console.log(res); +})(); +``` + +MIT \ No newline at end of file diff --git a/example/index.js b/example/index.js new file mode 100644 index 0000000000000000000000000000000000000000..0010c51d282a21b927667a596383e32b23539f87 --- /dev/null +++ b/example/index.js @@ -0,0 +1,10 @@ +const ServerChan = require('..'); + +const chan = new ServerChan({ + sckey: 'SCT82268Td2xus2Ms6YDvKbVIqaNNELzx' +}); + +(async () => { + const res = await chan.sendMessage('node-serverchan', 'hello world') + console.log(res); +})(); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000000000000000000000000000000000000..61b133af92b5e53314aeddb077b656c598fc1ed0 --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "@song940/serverchan", + "version": "0.0.0", + "description": "ServerChan API for Node.js", + "main": "serverchan.js", + "directories": { + "example": "example" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/song940/node-serverchan.git" + }, + "keywords": [ + "serverchan" + ], + "author": "Lsong", + "license": "MIT", + "bugs": { + "url": "https://github.com/song940/node-serverchan/issues" + }, + "homepage": "https://github.com/song940/node-serverchan#readme" +} \ No newline at end of file diff --git a/serverchan.js b/serverchan.js new file mode 100644 index 0000000000000000000000000000000000000000..2ed830d857db9227b6e73d9573cc4cc72ff5aa26 --- /dev/null +++ b/serverchan.js @@ -0,0 +1,45 @@ +const https = require('https'); +const assert = require('assert'); + +const { + SERVERCHAN_API, + SERVERCHAN_SENDKEY, +} = process.env; + +const get = url => + new Promise(done => https.get(url, done)); + +const readStream = res => new Promise((resolve, reject) => { + const buffer = []; + res + .on('error', reject) + .on('data', chunk => buffer.push(chunk)) + .on('end', () => resolve(Buffer.concat(buffer))) +}); + +/** + * ServerChan + * https://sct.ftqq.com + */ +class ServerChan { + constructor({ api = SERVERCHAN_API || 'https://sctapi.ftqq.com', sckey = SERVERCHAN_SENDKEY }) { + assert.ok(api); + assert.ok(sckey); + this.api = api; + this.sckey = sckey; + } + sendMessage(title, content) { + const { api, sckey } = this; + return Promise + .resolve() + .then(() => get(`${api}/${sckey}.send?title=${encodeURIComponent(title)}&desp=${encodeURIComponent(content)}`)) + .then(readStream) + .then(JSON.parse) + .then(res => { + assert.equal(res.code, 0, res.message); + return res.data; + }); + } +} + +module.exports = ServerChan; \ No newline at end of file