Repository navigation
Expand file tree
/
Copy pathbot.py
More file actions
266 lines (246 loc) · 10.5 KB
/
Copy pathbot.py
File metadata and controls
266 lines (246 loc) · 10.5 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import os
import io
import logging
from dotenv import load_dotenv
import qrcode
from PIL import Image
import barcode
from barcode.writer import ImageWriter
from io import BytesIO
import base64
import requests
from http.server import HTTPServer, BaseHTTPRequestHandler
import threading
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Application, CommandHandler, MessageHandler, CallbackQueryHandler, filters, ContextTypes
load_dotenv()
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
logger = logging.getLogger(__name__)
TOKEN = os.getenv("BOT_TOKEN")
if not os.path.exists('qr_codes'):
os.makedirs('qr_codes')
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
user = update.effective_user
welcome_message = (
f"👋 <b>Hello {user.first_name}!</b>\n\n"
"I'm QR Code Bot 🤖\n\n"
"📌 <b>What I can do:</b>\n"
"• Generate QR codes from text/links\n"
"• Read QR codes from images\n\n"
"⚙️ <b>Commands:</b>\n"
"/help - See how to use\n"
"/generate - Create QR code\n"
"/scan - Read QR from image\n"
"/batchqr - Generate multiple QR codes from a list"
)
await update.message.reply_text(welcome_message, parse_mode="HTML")
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
help_text = (
"🤖 <b>QR Code Bot Commands:</b>\n\n"
"<b>• Basic Commands:</b>\n"
"/start - Start the bot\n"
"/help - Show this help message\n\n"
"<b>• QR Code Operations:</b>\n"
"/generate - Generate a QR code\n"
"/scan - Scan QR code from image\n"
"/batchqr - Generate multiple QR codes from a list\n\n"
"📌 <b>How to use:</b>\n"
"1. <b>Generate QR:</b> Send /generate then enter text/URL\n"
"2. <b>Scan QR:</b> Send /scan then upload an image containing QR code\n"
"3. <b>Batch QR:</b> Send /batchqr then enter multiple texts/URLs\n\n"
"✨ <b>Features:</b>\n"
"• Supports URLs, text, contact info, WiFi credentials\n"
"• Customizable QR colors\n"
"• Batch QR generation\n"
)
await update.message.reply_text(help_text, parse_mode="HTML")
async def generate_qr(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_html(
"✨ <b>QR Code Generator</b>\n\n"
"Please send me the text or URL you want to encode in the QR code.\n\n"
"📝 <b>Examples:</b>\n"
"• <code>https://example.com</code>\n"
"• Your contact information\n"
"• WiFi: WPA2;SSID;Password\n"
"• Plain text message"
)
context.user_data['awaiting_qr_text'] = True
async def process_qr_text(update: Update, context: ContextTypes.DEFAULT_TYPE):
if context.user_data.get('awaiting_qr_text'):
text = update.message.text
keyboard = [
[
InlineKeyboardButton("⚫ Black (Default)", callback_data=f'color_black_{text}')
],
[
InlineKeyboardButton("🔵 Blue", callback_data=f'color_blue_{text}'),
InlineKeyboardButton("🔴 Red", callback_data=f'color_red_{text}'),
],
[
InlineKeyboardButton("🟢 Green", callback_data=f'color_green_{text}'),
InlineKeyboardButton("🟣 Purple", callback_data=f'color_purple_{text}')
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
await update.message.reply_html(
f"📝 <b>Text to encode:</b>\n<code>{text[:50]}{'...' if len(text) > 50 else ''}</code>\n\n"
"Choose QR code color:",
reply_markup=reply_markup
)
context.user_data['awaiting_qr_text'] = False
async def generate_qr_with_color(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
await query.answer()
data = query.data
color_name = data.split('_')[1]
text = '_'.join(data.split('_')[2:])
color_map = {
'black': (0, 0, 0),
'blue': (0, 0, 255),
'red': (255, 0, 0),
'green': (0, 128, 0),
'purple': (128, 0, 128)
}
fill_color = color_map.get(color_name, (0, 0, 0))
try:
await query.edit_message_text(f"🎨 Generating {color_name} QR code...")
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
qr.add_data(text)
qr.make(fit=True)
img = qr.make_image(fill_color=fill_color, back_color="white")
bio = io.BytesIO()
img.save(bio, 'PNG')
bio.seek(0)
await context.bot.send_photo(
chat_id=query.message.chat_id,
photo=bio,
caption=f"✅ <b>QR Code Generated!</b>\n\n"
f"<b>Content:</b> <code>{text[:100]}{'...' if len(text) > 100 else ''}</code>\n"
f"<b>Color:</b> {color_name.capitalize()}",
parse_mode="HTML"
)
except Exception as e:
logger.error(f"Error generating QR: {e}")
await query.edit_message_text("❌ Error generating QR code. Please try again.")
async def scan_qr(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_html(
"📸 <b>QR Code Scanner</b>\n\n"
"Please send me an image containing a QR code.\n\n"
"📌 <b>Tips:</b>\n"
"• Ensure good lighting\n"
"• QR code should be clear and centered\n"
"• Send as photo (not as file)"
)
context.user_data['awaiting_qr_image'] = True
async def process_image(update: Update, context: ContextTypes.DEFAULT_TYPE):
if context.user_data.get('awaiting_qr_image'):
try:
await update.message.reply_text("🔍 Scanning QR code")
photo_file = await update.message.photo[-1].get_file()
photo_bytes = io.BytesIO()
await photo_file.download_to_memory(photo_bytes)
photo_bytes.seek(0)
image = Image.open(photo_bytes)
response = requests.post(
'https://api.qrserver.com/v1/read-qr-code/',
files={'file': ('qr.png', photo_bytes.getvalue(), 'image/png')}
)
if response.status_code == 200:
data = response.json()
if data[0]['symbol'][0]['data']:
qr_data = data[0]['symbol'][0]['data']
if qr_data.startswith('http'):
formatted_data = f"🔗 <b>Link:</b> <code>{qr_data}</code>"
elif 'WIFI:' in qr_data.upper():
formatted_data = f"📶 <b>WiFi Config:</b> <code>{qr_data}</code>"
else:
formatted_data = f"📝 <b>Text:</b> <code>{qr_data}</code>"
await update.message.reply_html(
f"✅ <b>QR Code Detected!</b>\n\n{formatted_data}",
)
else:
await update.message.reply_text("❌ No QR code found in the image.")
else:
await update.message.reply_text("❌ Error scanning QR code. Please try again.")
except Exception as e:
logger.error(f"Error scanning QR: {e}")
await update.message.reply_text("❌ Error processing image. Please try again.")
context.user_data['awaiting_qr_image'] = False
async def button_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
await query.answer()
if query.data.startswith('color_'):
await generate_qr_with_color(update, context)
context.user_data['awaiting_qr_text'] = True
async def batch_qr(update: Update, context: ContextTypes.DEFAULT_TYPE):
if context.args:
texts = ' '.join(context.args).split(',')
if len(texts) > 5:
await update.message.reply_text("⚠️ Please limit to 5 QR codes at a time.")
return
await update.message.reply_text(f"Generating {len(texts)} QR codes...")
for i, text in enumerate(texts, 1):
text = text.strip()
if text:
qr = qrcode.make(text)
bio = io.BytesIO()
qr.save(bio, 'PNG')
bio.seek(0)
await update.message.reply_photo(
photo=bio,
caption=f"QR #{i}: <code>{text[:50]}{'...' if len(text) > 50 else ''}</code>",
parse_mode="HTML"
)
else:
await update.message.reply_html(
"📦 <b>Batch QR Generator</b>\n\n"
"Usage: /batchqr <code>text1, text2, text3</code>\n\n"
"Example: /batchqr <code>https://google.com, Hello World, WIFI:S:MyNetwork;T:WPA;P:mypassword;</code>",
)
async def error_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
logger.error(f"Update {update} caused error {context.error}")
if update and update.effective_message:
await update.effective_message.reply_text(
"❌ An error occurred. Please try again later."
)
def main():
app = Application.builder().token(TOKEN).build()
app.add_handler(CommandHandler("start", start))
app.add_handler(CommandHandler("help", help_command))
app.add_handler(CommandHandler("generate", generate_qr))
app.add_handler(CommandHandler("scan", scan_qr))
app.add_handler(CommandHandler("batchqr", batch_qr))
app.add_handler(CallbackQueryHandler(button_handler))
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, process_qr_text))
app.add_handler(MessageHandler(filters.PHOTO, process_image))
app.add_error_handler(error_handler)
print("🤖 QR Code Bot is starting...")
print("Press Ctrl+C to stop")
class HealthHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(b'Bot is alive!')
def log_message(self, format, *args):
pass # Silence logs
def run_health_server():
httpd = HTTPServer(('0.0.0.0', 10000), HealthHandler)
logger.info(f"✅ Health server on port 10000")
httpd.serve_forever()
health_thread = threading.Thread(target=run_health_server, daemon=True)
health_thread.start()
app.run_polling(
drop_pending_updates=True,
allowed_updates=Update.ALL_TYPES
)
if __name__ == '__main__':
main()