fetch
This commit is contained in:
parent
15aca99a85
commit
31be08bed5
|
@ -4,7 +4,7 @@
|
||||||
"description": "lul",
|
"description": "lul",
|
||||||
"main": "index.mjs",
|
"main": "index.mjs",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"start": "node --experimental-modules test.mjs"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
@ -13,7 +13,5 @@
|
||||||
"author": "Flummi",
|
"author": "Flummi",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"request": "^2.88.0",
|
|
||||||
"request-promise-native": "^1.0.5"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { getLevel } from "../inc/admin";
|
import { getLevel } from "../inc/admin";
|
||||||
import rp from "request-promise-native";
|
import fetch from "../inc/fetch";
|
||||||
import EventEmitter from "events";
|
import EventEmitter from "events";
|
||||||
|
|
||||||
export class tg extends EventEmitter {
|
export class tg extends EventEmitter {
|
||||||
|
@ -25,7 +25,8 @@ export class tg extends EventEmitter {
|
||||||
}
|
}
|
||||||
connect() {
|
connect() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
rp(`${this.api}/getMe`, { json: true })
|
fetch(`${this.api}/getMe`)
|
||||||
|
.then(res => res.json())
|
||||||
.then(res => {
|
.then(res => {
|
||||||
if(res.ok) {
|
if(res.ok) {
|
||||||
this.me = res.result;
|
this.me = res.result;
|
||||||
|
@ -48,7 +49,8 @@ export class tg extends EventEmitter {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
poll() {
|
poll() {
|
||||||
rp(`${this.api}/getUpdates?offset=${this.lastUpdate}&allowed_updates=message`, { json:true })
|
fetch(`${this.api}/getUpdates?offset=${this.lastUpdate}&allowed_updates=message`)
|
||||||
|
.then(res => res.json())
|
||||||
.then(res => {
|
.then(res => {
|
||||||
if(res.ok && res.result.length > 0) {
|
if(res.ok && res.result.length > 0) {
|
||||||
res = res.result[res.result.length-1];
|
res = res.result[res.result.length-1];
|
||||||
|
@ -76,17 +78,15 @@ export class tg extends EventEmitter {
|
||||||
return false;
|
return false;
|
||||||
const opts = {
|
const opts = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
uri: `${this.api}/sendMessage`,
|
|
||||||
body: {
|
body: {
|
||||||
chat_id: chatid,
|
chat_id: chatid,
|
||||||
text: msg,
|
text: msg,
|
||||||
parse_mode: "HTML"
|
parse_mode: "HTML"
|
||||||
},
|
}
|
||||||
json: true
|
|
||||||
};
|
};
|
||||||
if(reply)
|
if(reply)
|
||||||
opts.body.reply_to_message_id = reply;
|
opts.body.reply_to_message_id = reply;
|
||||||
rp(opts)
|
fetch(`${this.api}/sendMessage`, opts)
|
||||||
.then(res => {})
|
.then(res => {})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
});
|
});
|
||||||
|
|
19
src/inc/fetch.mjs
Normal file
19
src/inc/fetch.mjs
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
import http from "http";
|
||||||
|
import https from "https";
|
||||||
|
import url from "url";
|
||||||
|
|
||||||
|
export default (a, options = {}, link = url.parse(a)) => new Promise((resolve, reject) => {
|
||||||
|
(link.protocol === "https:"?https:http).get({...{
|
||||||
|
hostname: link.hostname,
|
||||||
|
path: link.path,
|
||||||
|
method: "GET"
|
||||||
|
}, ...options}, (res, data = "") => res
|
||||||
|
.setEncoding("utf8")
|
||||||
|
.on("data", chunk => data += chunk)
|
||||||
|
.on("end", () => resolve({
|
||||||
|
text: () => data,
|
||||||
|
json: () => { try { return JSON.parse(data); } catch(err) { return "no json D:"; } },
|
||||||
|
buffer: () => new Buffer.from(data)
|
||||||
|
}))
|
||||||
|
).on("error", err => reject(err));
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user