Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
184 lines (157 loc) · 4.88 KB
/
Copy pathindex.js
File metadata and controls
184 lines (157 loc) · 4.88 KB
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
const token = 'YOUR_TOKEN';
const https = require('https'); // or 'https' for https:// URLs
const fs = require('fs');
const sqlite = require('sqlite')
const sqlite3 = require('sqlite3')
const qrcode = require('qrcode-reader')
const qr = require('qr-image')
const Jimp = require('jimp')
const { Telegraf } = require('telegraf')
const bot = new Telegraf(token)
const db = sqlite.open({filename: "data.db", mode: sqlite3.OPEN_READWRITE, driver: sqlite3.Database})
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
const help_msg = `
<b>/encode - command to create QR code.
Send me photo to decipher it</b>
`
const createTable = `
CREATE TABLE IF NOT EXISTS users (
id INTEGER NOT NULL,
first_name TEXT,
state INTEGER NOT NULL
)
`
const saveUser = `
INSERT INTO users (id, first_name, state) VALUES (?, ?, ?)
`
const getUserById = `
SELECT * FROM users WHERE id = ?
`
const updateState = `
UPDATE users SET state = ? WHERE id = ?
`
const getUserState = `
SELECT state FROM users WHERE id = ?
`
function start_msg(msg)
{
var u_data = {'id': msg.from.id, 'f_name': msg.from.first_name, 'state': 0}
var text = `Hello ${u_data['f_name']}, its simple bot for decoding QR codes`
db.then(db => {
db.all(getUserById, msg.from.id).then(u => {
if(u.length == 0)
{
db.run(saveUser, u_data['id'], u_data['f_name'], u_data['state'])
bot.telegram.sendMessage(u_data['id'], text);
console.log('Success!')
} else {
console.log('User already exists!')
bot.telegram.sendMessage(u_data['id'], "*"+text+"*", {parse_mode: 'MarkdownV2'});
}
})
})
}
function checkState(msg)
{
var user_id = msg.from.id
db.then(db => {
db.get(getUserState, user_id).then(v => {
if(v.state == 1)
{
qr_encode(msg)
bot.telegram.sendPhoto(
user_id,
{source: './photos/qrcode.png'},
{caption: '<b>Created by @ksarp_qr_bot</b>', parse_mode: 'HTML',
reply_to_message_id: msg.message_id}
).catch(err => console.log(err))
// ctx.replyWithPhoto({source: './photos/qrcode.jpg'})
changeState(user_id, 0)
}
})
}).catch(err => console.log(err))
}
function changeState(user_id, state)
{
db.then(db => {
db.run(updateState, state, user_id)
})
}
function qr_encode(msg)
{
var qr_png = qr.image(msg.text, { type: 'png', size: 50 })
qr_png.pipe(fs.createWriteStream('./photos/qrcode.png'))
}
function qr_decode(msg, filename)
{
var file = fs.readFileSync(`photos/${filename}`)
var qr = new qrcode()
var options = {
parse_mode: 'HTML',
reply_to_message_id: msg.message.message_id
}
Jimp.read(file, function(err, image) {
if(err){
console.error(err)
bot.telegram.sendMessage(msg.from.id, `Error: ${err}`)
}
qr.callback = function(err, value) {
if (err) {
console.log(err)
bot.telegram.sendMessage(
msg.from.id,
`Error: <code>${err}</code>`, options
);
fs.unlinkSync(`photos/${filename}`)
}
if (value) {
bot.telegram.sendMessage(msg.from.id,
`<b>Decoded data:</b> <code>${value.result}</code>`,
options
)
fs.unlinkSync(`photos/${filename}`)
}
}
qr.decode(image.bitmap)
})
}
function downloadFile(link, ctx)
{
const file = fs.createWriteStream("photos/file.jpg");
const request = https.get(link, function(response) {
response.pipe(file);
// after download completed close filestream
file.on("finish", () => {
file.close();
qr_decode(ctx, "file.jpg")
console.log("Download Completed");
});
});
}
bot.start((ctx) => {
var chat_id = ctx.chat.id
start_msg(ctx.message)
})
bot.help((ctx) => {
var chat_id = ctx.chat.id
ctx.reply(help_msg, {reply_to_message_id: ctx.message.message_id, parse_mode: 'HTML'})
})
bot.on('message', (ctx) => {
var chat_id = ctx.chat.id
var msg_id = ctx.message_id
if(ctx.message.photo != undefined) {
var index = ctx.update.message.photo.length - 1
// delay(2500);
ctx.telegram.getFileLink(ctx.update.message.photo[index]).then(v => {
console.log(v.href);
downloadFile(v.href, ctx);
})
} else if(ctx.message.text == '/encode') {
changeState(ctx.from.id, 1)
ctx.reply('<b>Send me a text or link</b>', {reply_to_message_id: ctx.message.message_id, parse_mode: 'HTML'})
} else {
checkState(ctx.message)
}
})
console.log('started!')
bot.launch()