Skip to content

Commit 8195b6b

Browse files
committed
bug fixes
1 parent c19aa42 commit 8195b6b

File tree

10 files changed

+132
-32
lines changed

10 files changed

+132
-32
lines changed

api_calls/api_calls.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const axios = require('axios');
2+
const qs = require('qs');
3+
const stringUDFs = require('../string_udf/stringUDF');
4+
5+
class Calls extends stringUDFs {
6+
constructor(baseURL, headers) {
7+
super()
8+
this.baseURL = baseURL;
9+
this.headers = headers;
10+
}
11+
12+
// ************************************************************************************************
13+
// getAxiosData
14+
//
15+
// This will be get data via api calls
16+
// It will have six argument.
17+
// 1. Pass the endpoint route : >> Like as /public/data
18+
// 2. Optional : data >> default undefined, Pass the post body data
19+
// 3. Optional : urlencoded >> default true, Pass false if you have another type of post body data
20+
// 4. Optional : headers >> default will pickup from base constructor;
21+
// 5. Optional : base_URL >> default will pickup from base constructor;
22+
// 6. Optional : method >> default post, Pass the api call method like as get.
23+
//
24+
// Examples:
25+
//
26+
27+
// ************************************************************************************************
28+
getAxiosData = async (route, data = undefined, urlencoded = true, headers = undefined, base_URL = undefined, method = "post") => {
29+
let _this = this;
30+
31+
let _baseURL = base_URL ? base_URL : _this.baseURL;
32+
let url = _baseURL + route
33+
let _headers = headers ? headers : this.headers ? this.headers : {};
34+
let _data = urlencoded && data ? qs.stringify(data) : data;
35+
36+
//isValidURL
37+
if (!_this.isValidURL(url)) return { status: false, error: 'Invalid url.' };
38+
39+
let config = {
40+
method,
41+
url,
42+
headers: _headers,
43+
data: _data
44+
};
45+
46+
try {
47+
let data = await axios(config);
48+
return { status: true, data };
49+
} catch (e) {
50+
return { status: false, data: undefined, error: "Error" + e };
51+
}
52+
}
53+
}
54+
55+
module.exports = Calls

error_handler/express.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class ExpressError {
2+
constructor() {
3+
4+
}
5+
6+
// ************************************************************************************************
7+
// handleError
8+
//
9+
// Handle Error
10+
// It will have 4 argument.
11+
// 1. Pass the error.
12+
// 2. res >> Pass the express js response to reply to the sender.
13+
// 3. Optional : logMe >> default true, Log the error
14+
// 4. Optional : msg >> default undefined , Custom error message to reply; et.
15+
//
16+
// Examples:
17+
//
18+
// ************************************************************************************************
19+
expressHandleError = (error, res, logMe = true, msg = undefined) => {
20+
if (logMe) {
21+
console.log('handleError', error)
22+
}
23+
return res.status(400).json({ status: false, message: msg ? msg : 'Error occurred, Check log for more info.', error: "Error" + error });
24+
}
25+
26+
expressShowInfo = ({ originalUrl, body, params, file, files }, res, next) => {
27+
console.log('originalUrl', originalUrl);
28+
console.log('body', body);
29+
if (params) console.log('params', params);
30+
if (file) console.log('file', file);
31+
if (files) console.log('file', files);
32+
return next();
33+
};
34+
}

example/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,4 @@ Keep your token secure and store it safely, it can be used by anyone to control
7373
For a description of the Bot API, see this page: https://core.telegram.org/bots/api`;
7474
parseBOTToken = () => udf.parseBOTToken(msg)
7575

76-
parseBOTToken();
76+
// parseBOTToken();

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ const ConvertToSymbol = require("./algo/validation/convertsymbol");
66
const SqlUDF = require('./dbfn/sqlUDF');
77
const BroadCastMessage = require('./telegram/broadcast');
88
const Pushlog = require('./telegram/logs');
9-
const Quickfn = require('./telegram/quickfn');
9+
const QuickTelegramFn = require('./telegram/quickfn');
1010
const BhavCopy = require('./public_api/bhavcopy');
1111
const Cache = require("./misc/cache");
12+
const ApiCalls = require('./api_calls/api_calls');
1213

13-
class UDF extends Many(StringUDF, Table, InputValidation, ConvertToSymbol, SqlUDF, BroadCastMessage, Pushlog, Quickfn, BhavCopy, Cache) {
14+
class UDF extends Many(StringUDF, Table, InputValidation, ConvertToSymbol, SqlUDF, BroadCastMessage, Pushlog, QuickTelegramFn, BhavCopy, Cache, ApiCalls) {
1415
constructor(params) {
1516
super(params)
1617
this.params = params;

misc/fileuploading.js

Whitespace-only changes.

public_api/bhavcopy.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ class BhavCopy {
1515
this.customDir = dir && dir !== undefined && dir !== "undefined" ? dir : "";
1616
this.fileType =
1717
type &&
18-
type !== undefined &&
19-
type !== "undefined" &&
20-
this.__validateFileType().indexOf(type) !== -1 ?
21-
type :
22-
"csv";
18+
type !== undefined &&
19+
type !== "undefined" &&
20+
this.__validateFileType().indexOf(type) !== -1 ?
21+
type :
22+
"csv";
2323
this.isMultiplesFile = false;
2424
}
2525

string_udf/stringUDF.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class stringUDFs {
2-
constructor() {}
2+
constructor() { }
33

44
// ************************************************************************************************
55
// stringToJSON
@@ -71,13 +71,14 @@ class stringUDFs {
7171
// isAlphaNumeric('abcd xtyz5245/',true,true,true,'&^'); //Output will be - false;
7272
// isAlphaNumeric('abcd xtyz5245/',true,true,true,'/'); //Output will be - true;
7373
// ************************************************************************************************
74-
isAlphaNumeric(text, isSpaceAllowed = false, isAlphaAllowed = true, isNumericAllowed = true, allowedChars = null) {
74+
isAlphaNumeric(text, isSpaceAllowed = false, isAlphaAllowed = true,
75+
isNumericAllowed = true, allowedChars = undefined, maxLength = 100, minLength = 1) {
7576
var regex = "";
7677

7778
regex = isSpaceAllowed ? regex += " " : regex;
7879
regex = isAlphaAllowed ? regex += 'A-Za-z' : regex;
7980
regex = isNumericAllowed ? regex += '1-9' : regex;
80-
regex = allowedChars == null ? regex : regex += allowedChars;
81+
regex = allowedChars ? regex : regex += allowedChars;
8182
if (regex == "") return false;
8283

8384
let isValid = new RegExp(`\[${regex}\]\+\$`).test(text);
@@ -118,7 +119,7 @@ class stringUDFs {
118119
// properCase('abc'); //Output will be - Abc
119120
// ************************************************************************************************
120121
properCase(text) {
121-
let output = text.length > 0 ? text.charAt(0).toUpperCase() + text.substr(1).toLowerCase() : " ";
122+
let output = text.length > 0 ? text.charAt(0).toUpperCase() + text.substr(1).toLowerCase() : "";
122123
return output;
123124
}
124125

@@ -163,9 +164,9 @@ class stringUDFs {
163164
// ************************************************************************************************
164165
// exportArrayColumn
165166
//
166-
// This extract a column from multi-dimentional array
167+
// This extract a column from multi-dimensional array
167168
// It will have two argument.
168-
// 1. Pass the multi-dimentional array.
169+
// 1. Pass the multi-dimensional array.
169170
// 2. col >> the column number which needs to be exported.
170171

171172
// Examples:
@@ -191,7 +192,7 @@ class stringUDFs {
191192
//
192193
// ************************************************************************************************
193194
isObject(value) {
194-
return (typeof value === 'object');
195+
return (typeof value === 'object' && value !== null);
195196
}
196197

197198
// ************************************************************************************************
@@ -227,10 +228,12 @@ class stringUDFs {
227228
//
228229
// ************************************************************************************************
229230
isNoValue(value) {
230-
return (value === null || value == undefined || typeof value === 'undefined' || (isNaN(value) && !value.length));
231+
return (value === null || value || typeof value === 'undefined' || (isNaN(value) && !value.length));
231232
}
232233

233-
deleleColumns(arr, col = 0) {
234+
objectToArray = (data) => Array.isArray(data) ? data : [data];
235+
236+
deleteColumns(arr, col = 0) {
234237
if (col == 0) throw new Error('Argument col must not be zero.');
235238
if (col > arr[0].length) throw new Error('Delete column is greater than arr columns.');
236239

string_udf/table.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class Table extends StringUDF {
8181

8282
//slice column
8383
if (bunkColumn > 0) {
84-
array = this.deleleColumns(array, bunkColumn);
84+
array = this.deleteColumns(array, bunkColumn);
8585
}
8686

8787
//get each col length

telegram/broadcast.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
11
var Queue = require("better-queue");
2+
const Telegraf = require("telegraf");
3+
4+
const stringUDFs = require("../string_udf/stringUDF");
25

36
var q = new Queue(function (task, cb) {
4-
Promise.all(task).catch((e) => {});
7+
Promise.all(task).catch((e) => { });
58
cb(null, "done");
69
}, {
710
afterProcessDelay: 1100
811
})
912

10-
class BroadCastMessage {
11-
constructor() {}
13+
class BroadCastMessage extends stringUDFs {
14+
constructor() { }
1215

13-
broadcastMessages = (bot, users, text, params, msg_per_seconds = 25, isSameParams = true, isSameMessge = true) => {
16+
broadcastMessages = (BotOrToken, users, text, params, msg_per_seconds = 25, isSameParams = true, isSameMessage = true) => {
17+
let _this = this;
1418
//validate
19+
if (!BotOrToken) return new Error('A bot token or bot instance is required.');
1520
if (!Array.isArray(users)) return new Error('Argument users must be array.');
1621
if (msg_per_seconds > 29) return new Error('API limit is 30, Keep it 29 for safe side.');
1722
if (!isSameParams && params.length != users.length) return new Error('Users and params count is not equal.');
1823
if (!isSameParams && !Array.isArray(params)) return new Error('Argument params must be array.');
19-
if (!isSameMessge && text.length != users.length) return new Error('Users and text count is not equal.');
20-
if (!isSameMessge && !Array.isArray(text)) return new Error('Argument text must be array.');
24+
if (!isSameMessage && text.length != users.length) return new Error('Users and text count is not equal.');
25+
if (!isSameMessage && !Array.isArray(text)) return new Error('Argument text must be array.');
2126

27+
let _bot = _this.isObject(typeof BotOrToken === 'object' && BotOrToken !== null) ? BotOrToken :
28+
new Telegraf(BotOrToken).telegram;
2229

2330
for (let i = 0; i < users.length; i += msg_per_seconds) {
2431
const requests = users.slice(i, i + msg_per_seconds).map((chatId, index) => {
25-
const Kparams = isSameParams ? params : params[index];
26-
const msgTouser = isSameMessge ? text : text[index];
27-
return bot
28-
.sendMessage(chatId, msgTouser, Kparams) // function to send the msg.
29-
.catch((e) => {})
32+
const KParams = isSameParams ? params : params[index];
33+
const msgToUser = isSameMessage ? text : text[index];
34+
return _bot
35+
.sendMessage(chatId, msgToUser, KParams) // function to send the msg.
36+
.catch((e) => console.log(e))
3037
});
3138
q.push(requests);
3239
}

telegram/quickfn.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//const Markup = require("telegraf/markup");
22

3-
class Quickfn {
4-
constructor() {}
3+
class QuickTelegramFn {
4+
constructor() { }
55

66
cancelTask = async ({
77
session,
@@ -30,4 +30,4 @@ class Quickfn {
3030
parseBOTToken = (tokenMsg) => tokenMsg.match(/\d{6,11}:.+/g) || 0;
3131
}
3232

33-
module.exports = Quickfn;
33+
module.exports = QuickTelegramFn;

0 commit comments

Comments
 (0)