webfast 0.1.16 → 0.1.20

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,15 +1,205 @@
1
1
  // Here we can collect something by when user enter collect state in telegram
2
2
  module.exports = {
3
3
  line : {},
4
- set : function(id) {
4
+ set : function(id,data) {
5
5
  // To set que
6
- console.log(`Que Set Telegram`)
6
+ console.log(`Que Set Telegram`);
7
+ this.line[id] = data;
8
+ return this.line[id];
7
9
  },
8
- get : function(id) {
9
- console.log(`Que Get Telegram`)
10
+ get : function(id,program) {
11
+ console.log(`Que Get Telegram`);
12
+ // Check if in que line
13
+ if (program.modules.telegram.functions.que.line[id] == undefined) {
14
+ return false;
15
+ } else {
16
+ let lineData = program.modules.telegram.functions.que.line[id];
17
+ return lineData;
18
+ }
10
19
  },
11
20
  timer : function() {
12
21
  // Run timer
13
- console.log(`Timer Run`);
22
+ console.log(`Timer Run To See if we need to send a message or not`);
23
+ },
24
+ run : async function(program,command,chatID,middleValue,data) {
25
+ // Run timer
26
+ console.log(`run que data to give back response`);
27
+
28
+ // Check if que
29
+ try {
30
+
31
+ let scriptStart = `start`;
32
+ // It's new
33
+ if (program.modules.telegram.script.int[command] == undefined && program.modules.telegram.functions.que.line[chatID] == undefined) {
34
+ // Send message we don't know
35
+
36
+ return false;
37
+ }
38
+
39
+ // Create the que line command
40
+ let script = program.modules.telegram.script.int[command];
41
+
42
+ let current;
43
+
44
+ let theUUID = program.uuid.v4();
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
+ theUUID = current.uuid;
51
+
52
+ // Check what to do next first of all save anwser
53
+ const question = current.script[current.process];
54
+ // Grab function to process
55
+ const anwser = middleValue;
56
+
57
+ // Check if text then we need to match if not we probably need to run only function and go to next one
58
+ let matched = false;
59
+ let anwserData;
60
+ if (middleValue.text != undefined) {
61
+ console.log(`Match Text`);
62
+ // Loop through match
63
+ anwserData = middleValue.text;
64
+ for (let q in question.match.data) {
65
+ const qMatch = question.match.data[q];
66
+
67
+ // Check typeof
68
+ if (typeof qMatch.anwser == `object`) {
69
+ // Match in index
70
+ const matchedIndex = qMatch.anwser.indexOf(middleValue.text);
71
+ if (matchedIndex != -1) {
72
+ matched = qMatch;
73
+ }
74
+ } else if (qMatch.anwser == middleValue.text) {
75
+ matched = qMatch;
76
+ }
77
+ }
78
+ } else {
79
+ console.error(`Need to set anwser data`);
80
+ }
81
+
82
+ // End of match process fo matched checking
83
+ if (matched == false && current.q == true) {
84
+ // We have different data
85
+ console.error(`Not matched`);
86
+ // Send command when wrong // or buttons like reset button
87
+ // check what to do
88
+ return false;
89
+ } else {
90
+ console.log(`Matched`);
91
+ current.anwsers[current.process] = {
92
+ anwser : anwserData,
93
+ message : anwser,
94
+ ts : Date.now()
95
+ };
96
+
97
+ // Set next steps and get next steps data
98
+ try {
99
+ const nextData = current.script[matched.next];
100
+ if (nextData == undefined) {
101
+ throw new Error(`Problems getting nextData for`,current);
102
+ }
103
+ console.log(`Next Data`);
104
+ scriptStart = matched.next;
105
+ current.process = matched.next;
106
+ script = current.script;
107
+ } catch (err) {
108
+ console.error(err);
109
+ console.error(`Error getting data for next: ${matched.next}`);
110
+ }
111
+ }
112
+
113
+ console.log(`We have question anwser`);
114
+ } else {
115
+ // Create que line
116
+ let setData = {
117
+ ts : Date.now(),
118
+ action : command,
119
+ process : scriptStart,
120
+ script : script,
121
+ anwsers : {},
122
+ uuid : theUUID,
123
+ q : true
124
+ }
125
+ program.modules.telegram.functions.que.line[chatID] = setData;
126
+ current = setData;
127
+ }
128
+
129
+ // Get item
130
+ const theScript = script[scriptStart];
131
+
132
+ // Create replacelist
133
+ let replace = {
134
+ "{{URL}}" : process.env.url,
135
+ "{{TEST}}" : "TEST REPLACED"
136
+ }
137
+
138
+ // Check if anwsers
139
+ if (Object.keys(current.anwsers).length > 0) {
140
+ // Check the anwsers
141
+ for (let anwserTXT in current.anwsers) {
142
+ const typedOf = typeof current.anwsers[anwserTXT].anwser;
143
+
144
+ if (typedOf == `string`) {
145
+ replace[`{{${String(anwserTXT).toUpperCase()}}}`] = current.anwsers[anwserTXT].anwser;
146
+ }
147
+ }
148
+ console.log(`Check loop through anwsers`);
149
+ }
150
+
151
+ // Add items to replace
152
+ console.log(`Add Items to replace`);
153
+
154
+ // Check if text
155
+ let toSend = {}
156
+ if (theScript.object.text != undefined) {
157
+ toSend.text = theScript.object.text;
158
+ }
159
+
160
+ // Get object
161
+ if (theScript.object.image != undefined) {
162
+ console.log(`Send Image`);
163
+ toSend.image = theScript.object.image;
164
+ }
165
+
166
+ // Iterate through keys in toSend
167
+ for (let key in toSend) {
168
+ if (typeof toSend[key] === 'string') {
169
+ // Replace placeholders in the string globally
170
+ for (let placeholder in replace) {
171
+ toSend[key] = toSend[key].replace(new RegExp(placeholder, 'g'), replace[placeholder]);
172
+ }
173
+ }
174
+ }
175
+
176
+ // Check if text
177
+ let buttons;
178
+ // check for buttons
179
+ if (theScript.object.buttons != undefined) {
180
+ buttons = JSON.stringify(theScript.object.buttons);
181
+ // Loop through replace
182
+ for (let repKey in replace) {
183
+ const newRegx = new RegExp(repKey, 'g');
184
+ buttons = buttons.replace(newRegx,replace[repKey]);
185
+ }
186
+ buttons = JSON.parse(buttons);
187
+ }
188
+ await program.modules.telegram.functions.send(program,toSend,middleValue.chat.id,buttons);
189
+
190
+ // set que uuid
191
+ program.modules.telegram.functions.que.line[chatID] = current;
192
+
193
+ console.log(`We have script data`);
194
+
195
+ return current;
196
+ } catch (err) {
197
+ console.error(err);
198
+ console.error(`Error with que something`);
199
+ return false;
200
+ }
201
+
202
+ // Do all the things that we need to do in the run
203
+
14
204
  }
15
205
  }
@@ -0,0 +1,42 @@
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 == undefined && program.modules.telegram.functions.que.get(chatID,program) == false) {
8
+ await program.modules.telegram.functions.send(program,`Sorry we don't understand: ${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
+ // now it's just wait
21
+ console.log(`Went through que get`);
22
+ // Send message
23
+ if (queData == false) {
24
+ await program.modules.telegram.functions.send(program,`Sorry we don't know what to do with: ${command}`,chatID);
25
+ } else if (queData.process != `start`) {
26
+ // Process the que event
27
+ console.log(`Process the que event`);
28
+
29
+ }
30
+ }
31
+ console.log(`Setted`);
32
+
33
+ return true;
34
+ } catch(err) {
35
+ console.error(err);
36
+ console.error(`Error Scripting`);
37
+ }
38
+ },
39
+ response : async function() {
40
+ console.log(`Function Response`);
41
+ }
42
+ }
@@ -0,0 +1,57 @@
1
+ {
2
+ "input" : "test input",
3
+ "start" : {
4
+ "object" : {
5
+ "text" : "Test First Title : {{TEST}}",
6
+ "image" : "{{URL}}app/content/images/block.png"
7
+ },
8
+ "match" : {
9
+ "data" : [{
10
+ "anwser" : ["anwser text","anwser"],
11
+ "function" : "program.modules.bots.scripts.function.response",
12
+ "next" : "nextFunc"
13
+ },{
14
+ "anwser" : "other",
15
+ "function" : "program.modules.bots.scripts.function.response",
16
+ "next" : "nextFunc"
17
+ },{
18
+ "anwser" : "hi",
19
+ "function" : "program.modules.bots.scripts.function.response",
20
+ "next" : "nextFunc"
21
+ }]
22
+ },
23
+ "fail" : {
24
+ "response" : "start",
25
+ "function" : "program.modules.bots.scripts.function.response",
26
+ "next" : "failResponse"
27
+ }
28
+ },
29
+ "nextFunc" : {
30
+ "object" : {
31
+ "text" : "Testing Title : {{START}}",
32
+ "buttons" : [
33
+ [
34
+ { "text": "EventGO!", "web_app" : { "url" : "{{URL}}events/list"}},
35
+ { "text": "Create Event", "callback_data": "create_event" }
36
+ ]
37
+ ]
38
+ },
39
+ "match" : {
40
+ "data" : [{
41
+ "anwser" : ["create_event"],
42
+ "function" : "program.modules.bots.scripts.function.response",
43
+ "next" : "finish"
44
+ }]
45
+ },
46
+ "fail" : {
47
+ "response" : "start",
48
+ "function" : "program.modules.bots.scripts.function.response",
49
+ "next" : "failResponse"
50
+ }
51
+ },
52
+ "failResponse" : {
53
+ "object" : {
54
+ "text" : "Sorry Something Wnt wrong : {{START}}"
55
+ }
56
+ }
57
+ }
@@ -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,12 +29,38 @@ 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
+ } else {
48
+ telegramURL = `${telegramURL}/sendMessage`;
49
+ }
50
+
32
51
  // If options is there add as
33
52
  if (options != undefined) {
34
53
  body.reply_markup = options.reply_markup;
35
54
  }
36
55
 
37
56
  const madeRequest = await program.modules.request.post(program,telegramURL,body)
57
+
58
+ // Save to send so we can have the id and do things
59
+ madeRequest.result.uuid = program.uuid.v4();
60
+ let saveSend = await program.modules.data.findOrCreate(`eventgo`,`send`,{
61
+ message_id : madeRequest.result.message_id
62
+ },madeRequest.result);
63
+
38
64
  console.log(`Send Message`);
39
65
  return madeRequest;
40
66
  }
@@ -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.16",
3
+ "version": "0.1.20",
4
4
  "description": "WebFast! Bot Application, including TON mini-apps",
5
5
  "main": "index.js",
6
6
  "repository": {