-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (79 loc) · 3.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { Telegraf, Markup, session, } from "telegraf";
import { changeInlineKeyboard, checkUserID } from "./lib/util.js";
import { menu } from "./inline/Menu.js";
import { sendCatMenu } from "./inline/Categories.js";
import { sendAdminKeyboard } from "./inline/Admin.js";
import { testDatabase } from "./lib/db.js";
import { Sequelize } from "sequelize";
import { SynchroniseModels, defineModels } from "./lib/models.js";
import { Scenes } from "telegraf";
import { AddCatScene, UpdateCatScene,DeleteCatScene, AddProductScene, UpdateProductScene, DeleteProductScene} from "./inline/Admin.js";
import { ProductScene } from "./inline/Products.js";
import { CartScene, sendCartContents } from "./inline/Cart.js";
const db = new Sequelize(process.env.DBNAME, process.env.DBUSER, process.env.DBPASS, {
host: 'localhost',
dialect: 'mysql'
})
const bot = new Telegraf(process.env.TELEGRAMKEY);
const stage = new Scenes.Stage([AddCatScene, UpdateCatScene, DeleteCatScene, AddProductScene, UpdateProductScene, DeleteProductScene, ProductScene, CartScene])
const { Product, User, Category, CartItems, Order } = defineModels(db);
(async () => {
try {
bot.use(session())
bot.use(stage.middleware())
await db.authenticate();
console.log('Connection to database successful.');
await SynchroniseModels(db);
bot.start(async (ctx) => {
const chatId = ctx.chat.id;
const userId = ctx.from.id;
const userName = ctx.from.username;
const inlineKeyboard = Markup.inlineKeyboard([
Markup.button.callback("Yes", "Yes"),
Markup.button.callback("No", "No")
]);
await SynchroniseModels(db);
await checkUserID(userId, userName);
await ctx.reply("Welcome to the Commerce Bot! Would you like to shop?", inlineKeyboard);
});
bot.action('Yes', (ctx) => {
const chatId = ctx.chat.id;
console.log('Menu');
changeInlineKeyboard('Here is our Menu!', ctx, menu);
});
bot.action('No', (ctx) => {
ctx.editMessageText("Bot is now Closing. Thank you for shopping with us :).", {
reply_markup: undefined
});
});
bot.action('categories', (ctx) => {
sendCatMenu(ctx, bot);
});
bot.action('cart', async (ctx) => {
await sendCartContents(ctx);
});
bot.action('history', (ctx) => {
ctx.editMessageText('Fetching History Data...');
});
bot.action('admin', (ctx) => {
sendAdminKeyboard(ctx, bot);
});
bot.action('exit', (ctx) => {
ctx.editMessageText("Goodbye Customer! 😁", {
reply_markup: undefined
});
});
await bot.launch();
}catch (error) {
console.error('Error connecting to the database:', error);
}
})();
export {
bot,
db,
Product,
User,
Category,
CartItems,
Order
};