botql 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Bootstrap.js ADDED
@@ -0,0 +1,74 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Bootstrap.js — ponto de entrada do BotQL.
5
+ *
6
+ * Lê um ficheiro .sql (com suporte a IMPORT), cria o interpreter, liga os
7
+ * connectors reais das plataformas usadas via CONNECT, corre ON START e
8
+ * fica pronto a receber mensagens.
9
+ *
10
+ * Uso:
11
+ * node Bootstrap.js caminho/para/bot.sql
12
+ *
13
+ * Uso programático:
14
+ * const { bootstrap } = require('./Bootstrap.js');
15
+ * const bot = await bootstrap('bot.sql');
16
+ * await bot.receiveMessage('+244900000000', 'quero comprar');
17
+ */
18
+
19
+ const path = require('path');
20
+ const { BotQLInterpreter } = require('./botql.js');
21
+ const { ConnectorRegistry, attachConnectors } = require('./Connectors.js');
22
+ const { SQLiteDatabase } = require('./Database.js');
23
+
24
+ /**
25
+ * @param {string} sqlFilePath Caminho do ficheiro .sql principal do bot.
26
+ * @param {object} options
27
+ * @param {string} [options.dbPath] Caminho do ficheiro .sqlite. Sem isto,
28
+ * usa MemoryDatabase (sem persistência), que é o omisso do interpreter.
29
+ * @param {string} [options.connectorsPath] Caminho alternativo para o JSON
30
+ * de connectors. Por omissão usa o Connectors.json ao lado deste ficheiro.
31
+ * @param {boolean} [options.autoStart] Corre ON START automaticamente (omissão: true).
32
+ */
33
+ async function bootstrap(sqlFilePath, options = {}) {
34
+ const interpreterOptions = {};
35
+ if (options.dbPath) {
36
+ interpreterOptions.db = new SQLiteDatabase(options.dbPath);
37
+ }
38
+
39
+ const bot = BotQLInterpreter.fromFile(sqlFilePath, interpreterOptions);
40
+
41
+ const registry = new ConnectorRegistry(
42
+ options.connectorsPath || path.join(__dirname, 'Connectors.json')
43
+ );
44
+ attachConnectors(bot, registry);
45
+
46
+ if (options.autoStart !== false) {
47
+ await bot.start();
48
+ }
49
+
50
+ return bot;
51
+ }
52
+
53
+ // Execução direta: node Bootstrap.js caminho/bot.sql
54
+ if (require.main === module) {
55
+ const sqlFilePath = process.argv[2];
56
+ if (!sqlFilePath) {
57
+ console.error('Uso: node Bootstrap.js caminho/para/bot.sql');
58
+ process.exit(1);
59
+ }
60
+
61
+ bootstrap(sqlFilePath)
62
+ .then((bot) => {
63
+ console.log(`BotQL: bot "${bot.botName || '(sem nome)'}" iniciado.`);
64
+ if (!bot.running) {
65
+ console.log('Aviso: o ficheiro não tem "RUN BOT" — o bot foi carregado mas não marcado como em execução.');
66
+ }
67
+ })
68
+ .catch((err) => {
69
+ console.error(err.message);
70
+ process.exit(1);
71
+ });
72
+ }
73
+
74
+ module.exports = { bootstrap };
@@ -0,0 +1,17 @@
1
+ {
2
+ "TELEGRAM": "./connectors/telegram.js",
3
+ "WHATSAPP": "./connectors/whatsapp.js",
4
+ "DISCORD": "./connectors/discord.js",
5
+ "SLACK": "./connectors/slack.js",
6
+ "MESSENGER": "./connectors/messenger.js",
7
+ "INSTAGRAM": "./connectors/instagram.js",
8
+ "TWITTER": "./connectors/twitter.js",
9
+ "SMS": "./connectors/sms.js",
10
+ "EMAIL": "./connectors/email.js",
11
+ "WEBHOOK": "./connectors/webhook.js",
12
+ "VIBER": "./connectors/viber.js",
13
+ "LINE": "./connectors/line.js",
14
+ "WECHAT": "./connectors/wechat.js",
15
+ "MATRIX": "./connectors/matrix.js",
16
+ "GOOGLECHAT": "./connectors/googlechat.js"
17
+ }
package/botql-mode.js ADDED
@@ -0,0 +1,72 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Modo BotQL para CodeMirror 5 (addon/mode/simple.js).
5
+ *
6
+ * Usa exatamente a mesma lista de keywords do Parser.js (KEYWORDS),
7
+ * para nunca desalinhar highlight vs. sintaxe real aceite pelo parser.
8
+ *
9
+ * Uso (depois de incluir codemirror.js, mode/sql/sql.js NÃO é necessário,
10
+ * e addon/mode/simple.js):
11
+ *
12
+ * <script src="codemirror.js"></script>
13
+ * <script src="addon/mode/simple.js"></script>
14
+ * <script src="botql-mode.js"></script>
15
+ *
16
+ * const editor = CodeMirror(document.body, {
17
+ * mode: "botql",
18
+ * theme: "default"
19
+ * });
20
+ */
21
+
22
+ // Mesma lista do Parser.js — SQL real (CREATE, INSERT, UPDATE, ...)
23
+ // e BotQL (ON, WHEN, REPLY, ...) juntas, porque highlight não separa
24
+ // os dois mundos: é uma linguagem só.
25
+ const BOTQL_KEYWORDS = [
26
+ 'CREATE', 'BOT', 'PLATFORM', 'CONNECT', 'TABLE', 'PREVENT', 'DEFAULT',
27
+ 'ON', 'START', 'MESSAGE', 'FROM',
28
+ 'WHEN', 'CONTAINS', 'OR', 'OTHERWISE',
29
+ 'REPLY', 'TO', 'FORWARD', 'PARSE', 'SEND',
30
+ 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'SET', 'WHERE',
31
+ 'IMPORT', 'RUN'
32
+ ];
33
+
34
+ // Palavras especiais que merecem cor própria por serem "mágicas"
35
+ // no interpreter (botql.js), não por serem sintaxe da gramática.
36
+ const BOTQL_BUILTINS = ['Context', 'NOW', 'LAST_INSERT_ID'];
37
+
38
+ // Variáveis implícitas do runtime (client, message, lastMsg, SIGNAL).
39
+ const BOTQL_VARS = ['client', 'message', 'lastMsg', 'SIGNAL'];
40
+
41
+ function defineBotQLMode(CodeMirror) {
42
+ const keywordRegex = new RegExp('^(?:' + BOTQL_KEYWORDS.join('|') + ')\\b');
43
+ const builtinRegex = new RegExp('^(?:' + BOTQL_BUILTINS.join('|') + ')\\b');
44
+ const varRegex = new RegExp('^(?:' + BOTQL_VARS.join('|') + ')\\b');
45
+
46
+ CodeMirror.defineSimpleMode('botql', {
47
+ start: [
48
+ { regex: /--.*/, token: 'comment' },
49
+ { regex: /"(?:[^"\\]|\\.)*"?/, token: 'string' },
50
+ { regex: /'(?:[^'\\]|\\.)*'?/, token: 'string' },
51
+ { regex: /\b\d+(?:\.\d+)?\b/, token: 'number' },
52
+ { regex: keywordRegex, token: 'keyword' },
53
+ { regex: builtinRegex, token: 'builtin' },
54
+ { regex: varRegex, token: 'variable-2' },
55
+ { regex: /[A-Za-z_]\w*(?=\s*\()/, token: 'variable-2' }, // chamadas: NOME(
56
+ { regex: /[{}]/, token: 'bracket' },
57
+ { regex: /[()]/, token: 'bracket' },
58
+ { regex: /[+=,.]/, token: 'operator' },
59
+ { regex: /[A-Za-z_]\w*/, token: 'variable' }
60
+ ],
61
+ meta: {
62
+ lineComment: '--'
63
+ }
64
+ });
65
+ }
66
+
67
+ if (typeof module !== 'undefined' && module.exports) {
68
+ module.exports = { defineBotQLMode, BOTQL_KEYWORDS, BOTQL_BUILTINS, BOTQL_VARS };
69
+ }
70
+ if (typeof window !== 'undefined' && window.CodeMirror) {
71
+ defineBotQLMode(window.CodeMirror);
72
+ }