-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
215 lines (189 loc) · 8.29 KB
/
bot.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
require('dotenv').config();
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const TelegramBot = require('node-telegram-bot-api');
const crypto = require('crypto');
const { encrypt, decrypt } = require('./utils/encryption');
// Dynamic import for ES modules
let Account, AleoNetworkClient;
(async () => {
const aleoSDK = await import('@aleohq/sdk');
Account = aleoSDK.Account;
AleoNetworkClient = aleoSDK.AleoNetworkClient;
// Check for required environment variables
if (!process.env.MONGODB_URI || !process.env.TELEGRAM_BOT_TOKEN) {
console.error('Error: The MONGODB_URI and TELEGRAM_BOT_TOKEN environment variables must be set.');
process.exit(1);
}
// MongoDB connection with error handling
mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('Connected to MongoDB'))
.catch(err => {
console.error('Could not connect to MongoDB:', err.message);
process.exit(1);
});
// Define MongoDB user schema
const userSchema = new mongoose.Schema({
userId: String,
encryptedPrivateKey: String,
publicKey: String,
viewKey: String,
language: { type: String, default: 'en' },
notificationsEnabled: { type: Boolean, default: false },
});
// Method to handle wallet import and update
userSchema.statics.importOrUpdateWallet = async function(userId, privateKey, publicKey, viewKey) {
const encryptedPrivateKey = await encrypt(privateKey, userId);
const user = await this.findOne({ userId: userId });
if (user) {
user.encryptedPrivateKey = encryptedPrivateKey;
user.publicKey = publicKey;
user.viewKey = viewKey;
await user.save();
} else {
const newUser = new this({
userId: userId,
encryptedPrivateKey: encryptedPrivateKey,
publicKey: publicKey,
viewKey: viewKey,
});
await newUser.save();
}
};
// Compile the schema into a model
const User = mongoose.model('User', userSchema);
// Telegram bot setup
const bot = new TelegramBot(process.env.TELEGRAM_BOT_TOKEN, { polling: true });
const networkClient = new AleoNetworkClient('https://api.aleo.network');
// Start command
bot.onText(/^\/start$/, (msg) => {
bot.sendMessage(msg.chat.id, 'Welcome to Panthr-B Aleo Wallet Bot!');
});
// Help command updated with new features
bot.onText(/^\/help$/, (msg) => {
const helpMessage = `
Here are the commands you can use:
/start - Welcome message and bot information
/create_wallet - Create a new Aleo wallet
/import_wallet <privateKey> - Import an existing Aleo wallet
/balance <address> - Check the balance of a specified Aleo address (Coming soon)
/send_transaction <fromPrivateKey> <toAddress> <amount> - Send a transaction from one address to another (Coming soon)
/transaction_history <address> - Get the transaction history of a specified Aleo address (Coming soon)
/set_language <languageCode> - Set your preferred language for bot interactions
/toggle_notifications - Enable or disable notifications for your wallet
/price - Get the current price of Aleo tokens (Coming soon)
/network_stats - Display current Aleo network statistics (Coming soon)
/ai-aleo-learn - Learn about the Aleo network with an AI teaching machine (Coming soon)
/help - Show this help message
`;
bot.sendMessage(msg.chat.id, helpMessage, { parse_mode: 'Markdown' });
});
// Create wallet command
bot.onText(/^\/create_wallet$/, async (msg) => {
const account = new Account();
const privateKey = account.privateKey();
const viewKey = account.viewKey();
const address = account.address();
// Convert msg.from.id to a string before passing it to the encrypt function
const encryptedPrivateKey = await encrypt(privateKey, msg.from.id.toString());
const newUser = new User({
userId: msg.from.id.toString(), // Ensure userId is stored as a string
encryptedPrivateKey: encryptedPrivateKey,
publicKey: address,
viewKey: viewKey,
});
newUser.save()
.then(() => bot.sendMessage(msg.chat.id, `Wallet created successfully!\nAddress: ${address}\nPlease write down your private key and keep it safe.`))
.catch(err => bot.sendMessage(msg.chat.id, 'Error creating wallet. Please try again.'));
});
// Additional command handlers go here...
// AI Aleo Learn command
bot.onText(/\/ai-aleo-learn/, (msg) => {
const comingSoonMessage = `
*Coming soon: AI Aleo Learn*\n
This feature will provide an interactive learning experience to teach users about the Aleo network and its capabilities. Stay tuned for updates!
`;
bot.sendMessage(msg.chat.id, comingSoonMessage, { parse_mode: 'Markdown' });
});
// Set language command
bot.onText(/\/set_language (.+)/, async (msg, match) => {
const language = match[1].toLowerCase();
const supportedLanguages = ['en', 'es', 'fr']; // Add more supported languages as needed
if (supportedLanguages.includes(language)) {
await User.updateOne({ userId: msg.from.id }, { language: language });
bot.sendMessage(msg.chat.id, `Language updated to ${language}.`);
} else {
bot.sendMessage(msg.chat.id, `Language not supported. Please choose from the following: ${supportedLanguages.join(', ')}`);
}
});
// Toggle notifications command
bot.onText(/\/toggle_notifications/, async (msg) => {
const user = await User.findOne({ userId: msg.from.id });
if (user) {
const newNotificationSetting = !user.notificationsEnabled;
await User.updateOne({ userId: msg.from.id }, { notificationsEnabled: newNotificationSetting });
bot.sendMessage(msg.chat.id, `Notifications have been ${newNotificationSetting ? 'enabled' : 'disabled'}.`);
} else {
bot.sendMessage(msg.chat.id, `User not found. Please create a wallet first.`);
}
});
// View private key command
bot.onText(/\/view_private_key/, async (msg) => {
const user = await User.findOne({ userId: msg.from.id });
if (user) {
const decryptedPrivateKey = await decrypt(user.encryptedPrivateKey, msg.from.id);
bot.sendMessage(msg.chat.id, `Your private key is: ${decryptedPrivateKey}`);
} else {
bot.sendMessage(msg.chat.id, `No wallet found. Please create or import a wallet first.`);
}
});
// Import wallet command
bot.onText(/^\/import_wallet (.+)$/, async (msg, match) => {
const privateKey = match[1];
try {
const account = new Account(privateKey);
const viewKey = account.viewKey();
const address = account.address();
// Convert msg.from.id to a string before passing it to the encrypt function
const encryptedPrivateKey = await encrypt(privateKey, msg.from.id.toString());
// Use the importOrUpdateWallet method to encapsulate the update or create logic
await User.importOrUpdateWallet(msg.from.id.toString(), privateKey, address, viewKey);
bot.sendMessage(msg.chat.id, `Wallet imported successfully!\nAddress: ${address}`);
} catch (error) {
bot.sendMessage(msg.chat.id, `Error importing wallet. Please check your private key and try again.`);
}
});
// Price tracking command
bot.onText(/\/price/, (msg) => {
// Placeholder for price tracking logic
bot.sendMessage(msg.chat.id, `Price tracking feature is coming soon.`);
});
// Network stats command
bot.onText(/\/network_stats/, (msg) => {
// Placeholder for network stats logic
bot.sendMessage(msg.chat.id, `Network stats feature is coming soon.`);
});
// Placeholder for balance check command
bot.onText(/\/balance (.+)/, async (msg, match) => {
// Implement balance check logic when available
bot.sendMessage(msg.chat.id, `Balance check feature is coming soon.`);
});
// Placeholder for send transaction command
bot.onText(/\/send_transaction (.+) (.+) (.+)/, async (msg, match) => {
// Implement send transaction logic when available
bot.sendMessage(msg.chat.id, `Send transaction feature is coming soon.`);
});
// Placeholder for transaction history command
bot.onText(/\/transaction_history (.+)/, async (msg, match) => {
// Implement transaction history logic when available
bot.sendMessage(msg.chat.id, `Transaction history feature is coming soon.`);
});
// Connect to MongoDB and start the bot
mongoose.connection.once('open', () => {
console.log('Connected to MongoDB');
bot.startPolling();
console.log('Bot started');
}).on('error', (error) => {
console.error('MongoDB connection error:', error);
});
})();