webfast 0.1.14 → 0.1.18

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,107 @@
1
+ // Here we can collect something by when user enter collect state in telegram
2
+ module.exports = {
3
+ line : {},
4
+ set : function(id,data) {
5
+ // To set que
6
+ console.log(`Que Set Telegram`);
7
+ this.line[id] = data;
8
+ return this.line[id];
9
+ },
10
+ get : function(id) {
11
+ console.log(`Que Get Telegram`);
12
+ // Check if in que line
13
+ let indexPart = Object.keys(this.line).indexOf(id);
14
+ if (indexPart == -1) {
15
+ return false;
16
+ } else {
17
+ let lineData = this.line[indexPart];
18
+ return lineData;
19
+ }
20
+ },
21
+ timer : function() {
22
+ // Run timer
23
+ console.log(`Timer Run To See if we need to send a message or not`);
24
+ },
25
+ run : async function(program,command,chatID,middleValue,data) {
26
+ // Run timer
27
+ console.log(`run que data to give back response`);
28
+
29
+ // Check if que
30
+ try {
31
+
32
+ let scriptStart = `start`;
33
+ // It's new
34
+ if (program.modules.telegram.script.int[command] == undefined) {
35
+ // Send message we don't know
36
+
37
+ return false;
38
+ }
39
+
40
+ // Create the que line command
41
+ const script = program.modules.telegram.script.int[command];
42
+
43
+ let current;
44
+
45
+ if (program.modules.telegram.functions.que.line[chatID] != undefined) {
46
+ // It's original que
47
+ // Process response
48
+ console.log(`It's a process`)
49
+ current = program.modules.telegram.functions.que.line[chatID];
50
+ } else {
51
+ // Create que line
52
+ let setData = {
53
+ ts : Date.now(),
54
+ action : command,
55
+ process : scriptStart,
56
+ script : script,
57
+ anwsers : {}
58
+ }
59
+ program.modules.telegram.functions.que.line[chatID] = setData;
60
+ current = setData;
61
+ }
62
+
63
+ // Get item
64
+ const theScript = script[scriptStart];
65
+
66
+ // Create replacelist
67
+ let replace = {
68
+ "{{URL}}" : process.env.url,
69
+ "{{TEST}}" : "TEST REPLACED"
70
+ }
71
+
72
+ // Check if text
73
+ let toSend = {}
74
+ if (theScript.object.text != undefined) {
75
+ toSend.text = theScript.object.text;
76
+ }
77
+
78
+ // Get object
79
+ if (theScript.object.image != undefined) {
80
+ console.log(`Send Image`);
81
+ toSend.image = theScript.object.image;
82
+ }
83
+
84
+ // Iterate through keys in toSend
85
+ for (let key in toSend) {
86
+ if (typeof toSend[key] === 'string') {
87
+ // Replace placeholders in the string globally
88
+ for (let placeholder in replace) {
89
+ toSend[key] = toSend[key].replace(new RegExp(placeholder, 'g'), replace[placeholder]);
90
+ }
91
+ }
92
+ }
93
+
94
+ // Check if text
95
+ await program.modules.telegram.functions.send(program,toSend,middleValue.chat.id);
96
+
97
+ program.modules.telegram.functions.que.line[chatID] = current;
98
+ console.log(`We have script data`);
99
+ } catch (err) {
100
+ console.error(err);
101
+ console.error(`Error with que something`);
102
+ }
103
+
104
+ // Do all the things that we need to do in the run
105
+
106
+ }
107
+ }
@@ -0,0 +1,31 @@
1
+ module.exports = {
2
+ check : async function (program,command,chatID,middleValue,received) {
3
+ // Checkif there is que
4
+ console.log(`Check if there is script`);
5
+ try {
6
+ const scriptData = program.modules.telegram.script.int[command];
7
+ if (scriptData.length == 0) {
8
+ await program.modules.telegram.functions.send(program,`${command}`,chatID,[
9
+ [
10
+ { text: 'EventGO!', web_app : { url : 'https://cloud.eventgo.today/events/list'}},
11
+ { text: 'Create Event', callback_data: 'create_event' },
12
+ ]
13
+ ]);
14
+ } else {
15
+ // We have some script so check if we have in que
16
+ const queData = await program.modules.telegram.functions.que.run(program,command,chatID,middleValue,received);
17
+
18
+ // Check if queData is empty otherwise we will run the script from "start", if que data is not empty we run it further
19
+ // unt o process script
20
+
21
+ console.log(`Went through que get`);
22
+ }
23
+ console.log(`Setted`);
24
+
25
+ return true;
26
+ } catch(err) {
27
+ console.error(err);
28
+ console.error(`Error Scripting`);
29
+ }
30
+ }
31
+ }
@@ -0,0 +1,37 @@
1
+ {
2
+ "input" : "test input",
3
+ "start" : {
4
+ "object" : {
5
+ "text" : "Testing Title : {{TEST}}",
6
+ "image" : "{{URL}}app/content/images/block.png"
7
+ },
8
+ "match" : {
9
+ "data" : [{
10
+ "response" : "anwser text",
11
+ "function" : "program.modules.bots.scripts.function",
12
+ "reply_markup" : {
13
+ "inline_keyboard": [[
14
+ {
15
+ "text": "A",
16
+ "callback_data": "A1"
17
+ },
18
+ {
19
+ "text": "B",
20
+ "callback_data": "C1"
21
+ }]
22
+ ]
23
+ },
24
+ "next" : "nextFunc"
25
+ },{
26
+ "response" : "other",
27
+ "function" : "program.modules.bots.scripts.function",
28
+ "next" : "nextFunc"
29
+ }]
30
+ },
31
+ "fail" : {
32
+ "response" : "start",
33
+ "function" : "program.modules.bots.scripts.function",
34
+ "next" : "failResponse"
35
+ }
36
+ }
37
+ }
@@ -1,7 +1,7 @@
1
1
  module.exports = async function(program,message,id,buttons) {
2
2
  console.log(`Telegram Send`);
3
3
  // Create Request for send
4
- const telegramURL = `https://api.telegram.org/bot${process.env.telegram}/sendMessage`;
4
+ let telegramURL = `https://api.telegram.org/bot${process.env.telegram}`;
5
5
 
6
6
  // Check if buttons is send as []
7
7
  let options;
@@ -29,6 +29,23 @@ module.exports = async function(program,message,id,buttons) {
29
29
  parse_mode : 'HTML'
30
30
  }
31
31
 
32
+ // Check if object
33
+ if (typeof message == `object`) {
34
+ if (message.text != undefined && message.image == undefined) {
35
+ body.text = message.text;
36
+ telegramURL = `${telegramURL}/sendMessage`;
37
+ }
38
+
39
+ // Check for image
40
+ if (message.image != undefined) {
41
+ body.photo = message.image;
42
+ body.caption = message.text;
43
+ delete body.text;
44
+ delete body.reply_to_message_id;
45
+ telegramURL = `${telegramURL}/sendPhoto`;
46
+ }
47
+ }
48
+
32
49
  // If options is there add as
33
50
  if (options != undefined) {
34
51
  body.reply_markup = options.reply_markup;
@@ -53,6 +53,19 @@ module.exports = async function(program,folder) {
53
53
  }
54
54
 
55
55
  middleValue.chat.uuid = user.uuid;
56
+
57
+ // Find or create to add message to db
58
+ body.uuid = program.uuid.v4();
59
+ let received = await program.modules.data.findOrCreate(`eventgo`,`received`,{
60
+ update_id : body.update_id
61
+ },body);
62
+
63
+ // Set body from receivd
64
+ body.uuid = received.uuid;
65
+
66
+
67
+ console.log(`We have received message`,received);
68
+
56
69
  try {
57
70
  if (middleValue.text.startsWith('/')) {
58
71
  // If it starts with a slash, it might be a command
@@ -69,18 +82,20 @@ module.exports = async function(program,folder) {
69
82
  // Let's split up
70
83
  let match = [];
71
84
 
72
- let splitVariables = variables.split(`-`);
85
+ if (variables != undefined) {
86
+ let splitVariables = variables.split(`-`);
73
87
 
74
- if (splitVariables.length > 4) {
75
- match[0] = splitVariables[0];
76
- if (splitVariables.length >= 5) {
77
- match[1] = splitVariables[splitVariables.length-1];
78
- let uuid = variables.replace(`${match[0]}-`,``).replace(`-${match[1]}`,``);;
79
- match[2] = uuid;
88
+ if (splitVariables.length > 4) {
89
+ match[0] = splitVariables[0];
90
+ if (splitVariables.length >= 5) {
91
+ match[1] = splitVariables[splitVariables.length-1];
92
+ let uuid = variables.replace(`${match[0]}-`,``).replace(`-${match[1]}`,``);;
93
+ match[2] = uuid;
94
+ }
80
95
  }
81
96
  }
82
97
 
83
- if (match) {
98
+ if (match.length > 0) {
84
99
  // Extract the parts
85
100
  const action = match[0];
86
101
  const uuid = match[2];
@@ -93,10 +108,22 @@ module.exports = async function(program,folder) {
93
108
  // It's something to do check if we can find this in applications
94
109
  try {
95
110
  console.log(`Run Sub Function`);
96
- await program.modules.telegram.functions[action](program,key,action,uuid,subFunction,middleValue);
111
+ let resp = await program.modules.telegram.functions[action](program,key,action,uuid,subFunction,middleValue,received);
112
+
113
+ // Switch resp;
114
+ console.log(`Check if we send message`);
115
+ switch (resp.response) {
116
+ case `message`:
117
+ console.log(`We have response, check for response message`);
118
+ const message = respFunc[action];
119
+ await program.modules.telegram.functions.send(program,message,middleValue.chat.id);
120
+ break;
121
+ default:
122
+ console.error(`Missing Response Action Telegram: ${action}`);
123
+ }
97
124
  } catch (err) {
98
125
  console.error(err);
99
- console.error(`Error Sub Function`);
126
+ console.error(`Error Sub Function ${key}, ${action}`);
100
127
  }
101
128
 
102
129
  } else {
@@ -104,7 +131,7 @@ module.exports = async function(program,folder) {
104
131
  // So run the function
105
132
  try {
106
133
  // Run dynamic the type of middleware
107
- const runFunc = await program.modules.telegram.middleware[key][command](req,res,body,params,command,middleValue);
134
+ const runFunc = await program.modules.telegram.middleware[key][command](req,res,body,params,command,middleValue,received);
108
135
  const respFunc = runFunc.response;
109
136
  // PRocess response for object
110
137
  const action = Object.keys(respFunc)[0];
@@ -156,12 +183,16 @@ module.exports = async function(program,folder) {
156
183
  } catch (err) {
157
184
  //console.error(err);
158
185
  //console.error(`Error For Telegram Function`);
186
+ console.log(`Checking for script`);
187
+ const scripting = await program.modules.telegram.script.function.check(program,command,middleValue.chat.id,middleValue,received);
188
+ /*
159
189
  await program.modules.telegram.functions.send(program,`${command}`,middleValue.chat.id,[
160
190
  [
161
191
  { text: 'EventGO!', web_app : { url : 'https://cloud.eventgo.today/events/list'}},
162
192
  { text: 'Create Event', callback_data: 'create_event' },
163
193
  ]
164
- ]);
194
+ ]);*/
195
+ console.log(scripting);
165
196
  }
166
197
 
167
198
 
@@ -170,8 +201,6 @@ module.exports = async function(program,folder) {
170
201
  }
171
202
  } catch (message) {
172
203
  // Process as other
173
- res.send(`OK | ${command} | ${variables}`)
174
- res.status(200);
175
204
  console.log(`Process Different`);
176
205
  let checkArray = [`location`];
177
206
  // Loop through checkArray
@@ -209,6 +238,11 @@ module.exports = async function(program,folder) {
209
238
  }
210
239
  }
211
240
  }
241
+
242
+ console.log(`We do something here`);
243
+
244
+ res.send(`OK `)
245
+ res.status(200);
212
246
  }
213
247
  } catch (err) {
214
248
  console.error(err);
@@ -298,6 +332,84 @@ module.exports = async function(program,folder) {
298
332
  // Add middleware
299
333
  telegram.middleware = middleWarFuncs;
300
334
 
335
+ // Process scripts
336
+ const scriptsPath = program.path.join(__dirname,`telegram`,`scripts`);
337
+
338
+ // Loop Through scripts folder
339
+ let scriptsData = await program.modules.walkDirectory(scriptsPath);
340
+
341
+ // Let's loop througha and if no extension it's folder
342
+ let allScripts = {
343
+ int : {}
344
+ }
345
+ for (let scriptIndex in scriptsData) {
346
+ let script = scriptsData[scriptIndex];
347
+ // We now have the specific script check if folder
348
+ if (!script.extension) {
349
+ // It's folder create the function and read folder
350
+ console.log(`It's folder check for files as in .json or .js`);
351
+ const folderScriptScan = await program.modules.walkDirectory(script.path);
352
+
353
+ // Create now allScripts item interaction as we will check dynamic for the item
354
+ for (let fsi in folderScriptScan) {
355
+ // So now again we only do things when it's a file but check extension
356
+ const scriptItem = folderScriptScan[fsi];
357
+ // We have folder data
358
+ if (!scriptItem.extension) {
359
+ continue;
360
+ }
361
+
362
+ // We have script create
363
+ console.log(`Folder Script Scan`,scriptItem);
364
+
365
+ // Switch extension
366
+ const extCheck = scriptItem.extension.slice(1);
367
+
368
+ // We have the extension so swich between those
369
+ switch (extCheck){
370
+ case `json`:
371
+ console.log(`Read File`);
372
+ try {
373
+ const readFile = JSON.parse(await program.fs.readFileSync(scriptItem.path,`utf-8`));
374
+
375
+ // Create the command
376
+ const interact = readFile.input;
377
+
378
+ // For interaction
379
+ delete readFile.input;
380
+ allScripts.int[interact] = readFile;
381
+
382
+ console.log(`We have readed the file`);
383
+ } catch (err) {
384
+ console.error(err);
385
+ console.error(`Error Reading JSON file`);
386
+ }
387
+ break;
388
+ default:
389
+ // When it's not like json
390
+ console.log(`it's something else`);
391
+ }
392
+ }
393
+
394
+ console.log(`Walk Through folder and check for script.json`);
395
+
396
+ } else {
397
+ // It's main script functions
398
+ console.log(`Main Script Function In Specific folder`);
399
+ try {
400
+ // Add script to allscript
401
+ allScripts[script.name] = require(script.path);
402
+ console.log(`Script : ${script.name} - loaded`);
403
+ } catch (err) {
404
+ console.error(err);
405
+ console.error(`Error Loading script`,script);
406
+ }
407
+ }
408
+ }
409
+
410
+ // Set all script
411
+ telegram.script = allScripts;
412
+
301
413
  program.modules.telegram = telegram;
302
414
  return program;
303
415
  }
@@ -44,7 +44,7 @@ module.exports = async function(db, collection, condition, dataToCreate) {
44
44
  if (result.acknowledged === true) {
45
45
  console.log('New document created:', result.insertedId);
46
46
  const existingDocument = await collection.findOne(condition);
47
- return existingDocument;
47
+ return dataToCreate;
48
48
  } else {
49
49
  console.error('Failed to create a new document.');
50
50
  return null;
@@ -15,12 +15,17 @@ module.exports = async function(program, url, body) {
15
15
 
16
16
  // Using standard fetch function
17
17
  const response = await fetch(url, theOptions);
18
+ // Get response body
19
+ const respBody = await response.json(); // Assuming the response is JSON
20
+
21
+ // Get response headers
22
+ const respHeaders = response.headers;
18
23
 
19
24
  if (!response.ok) {
20
25
  throw new Error(`HTTP error! Status: ${response.status}`);
21
26
  }
22
27
 
23
- const responseData = await response.json();
28
+ const responseData = respBody;
24
29
  console.log('Response Data:', responseData);
25
30
 
26
31
  // Return response data or true to indicate success
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webfast",
3
- "version": "0.1.14",
3
+ "version": "0.1.18",
4
4
  "description": "WebFast! Bot Application, including TON mini-apps",
5
5
  "main": "index.js",
6
6
  "repository": {