MohammedAlakhras commited on
Commit
ff0cc88
·
1 Parent(s): f3cea98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -17
app.py CHANGED
@@ -6,24 +6,25 @@ from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
6
  from config import api_key
7
 
8
  # تعريف الدالة الخاصة برفع الملفات
9
- def upload_file(update, context):
10
- # الحصول على رابط الملف من رسالة المستخدم
11
- file_url = update.message.text.split()[1]
12
- # جلب الملف من الرابط المحدد
 
13
  file = requests.get(file_url)
14
- # توليد اسم الملف العشوائي
15
- file_name = str(update.message.from_user.id) + '_' + str(update.message.date.timestamp()) + '_' + str(os.urandom(4).hex()) + '.jpg'
16
- # حفظ الملف المحمل مؤقتاً
17
- with open(file_name, 'wb') as f:
18
- f.write(file.content)
19
- # توليد صورة مصغرة للملف المحمل
20
- thumb = Image.open(BytesIO(file.content))
21
- thumb.thumbnail((128, 128))
22
- # إرسال الملف والصورة المصغرة إلى المستخدم
23
- bot.send_photo(chat_id=update.message.chat_id, photo=open(file_name, 'rb'), caption='Uploaded file', reply_to_message_id=update.message.message_id)
24
- bot.send_photo(chat_id=update.message.chat_id, photo=thumb, caption='Thumbnail', reply_to_message_id=update.message.message_id)
25
- # حذف الملف المحمل مؤقتاً
26
- os.remove(file_name)
27
 
28
  # تعريف الدالة الخاصة بإرسال رسالة استجابة
29
  def reply_message(update, context ):
 
6
  from config import api_key
7
 
8
  # تعريف الدالة الخاصة برفع الملفات
9
+ def upload_file(bot, update):
10
+ # Get the file URL from the user message
11
+ file_url = update.message.text
12
+
13
+ # Download the file from the URL
14
  file = requests.get(file_url)
15
+
16
+ # Upload the file to Telegram using the bot API
17
+ url = f"https://api.telegram.org/bot{bot.token}/sendDocument"
18
+ data = {"chat_id": update.message.chat_id}
19
+ files = {"document": file.content}
20
+ response = requests.post(url, data=data, files=files)
21
+
22
+ # Check if the file was uploaded successfully
23
+ if response.status_code == 200:
24
+ update.message.reply_text("File uploaded successfully to Telegram!")
25
+ else:
26
+ update.message.reply_text("Failed to upload file to Telegram.")
27
+
28
 
29
  # تعريف الدالة الخاصة بإرسال رسالة استجابة
30
  def reply_message(update, context ):