MohammedAlakhras commited on
Commit
fb5dadd
ยท
1 Parent(s): 07cc25a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -28
app.py CHANGED
@@ -1,43 +1,63 @@
1
  import gradio as gr
2
  from telethon.sync import TelegramClient, events
3
- import asyncio
4
  import datetime
 
 
5
 
6
- api_id = '10086982'
7
- api_hash = '3ed461889a88b31fcbc323d13e43cf7e'
 
 
8
 
 
 
 
 
 
 
 
 
 
9
 
10
- def handle_messages():
11
- async def process_messages():
12
- async with TelegramClient('anon', api_id, api_hash) as client:
13
- @client.on(events.NewMessage())
14
- async def my_event_handler(event):
15
- sender = await event.get_sender()
16
- sender_id = sender.id
17
- sender_name = sender.first_name
18
- chat = await event.get_chat()
19
- chat_id = chat.id
20
- text = event.raw_text
21
 
22
- # ู…ู‚ูŠุงุณ ุงู„ุฒู…ู†
23
- checkpoint = datetime.datetime.now()
24
- print(f'Checkpoint: {checkpoint}')
25
 
26
- # ุฑุณุงู„ุฉ ุดุฎุตูŠุฉ
27
- if chat_id == sender_id:
28
- response = f'ุงู‡ู„ุง {sender_name} , ู„ู‚ุฏ ุชู„ู‚ูŠุช ุฑุณุงู„ุชูƒ ุณุฃู‚ูˆู… ุจุงู„ุฑุฏ ุนู„ูŠูƒ ุจุฃู‚ุฑุจ ูˆู‚ุช ู…ู…ูƒู† ูˆุดูƒุฑุงู‹ ู„ุชูู‡ู…ูƒ.'
29
- await client.send_message(chat_id, response)
30
-
31
- # ุฑุณุงู„ุฉ ู…ุฌู…ูˆุนุฉ
32
- elif '@Mohammed_Alakhras' in text:
33
- response = f'ุงู‡ู„ุง {sender_name} , ู„ู‚ุฏ ุชู„ู‚ูŠุช ุฑุณุงู„ุชูƒ ุณุฃู‚ูˆู… ุจุงู„ุฑุฏ ุนู„ูŠูƒ ุจุฃู‚ุฑุจ ูˆู‚ุช ู…ู…ูƒู† ูˆุดูƒุฑุงู‹ ู„ุชูู‡ู…ูƒ.'
34
- await client.send_message(chat_id, response)
35
 
 
 
 
36
 
37
- await client.run_until_disconnected()
 
 
 
 
 
 
 
38
 
39
- asyncio.run(process_messages())
 
 
 
 
 
 
40
 
 
41
  # ุชุนุฑูŠู ุญู‚ูˆู„ ุงู„ู†ุต ููŠ Gradio
42
 
43
  # ุชุนุฑูŠู ุงู„ูˆุงุฌู‡ุฉ
 
1
  import gradio as gr
2
  from telethon.sync import TelegramClient, events
 
3
  import datetime
4
+ import socks
5
+ import time
6
 
7
+ proxy_server = '142.93.68.63'
8
+ proxy_port = 2434
9
+ proxy_secret = 'ee32b920dffb51643028e2f6b878d4eac1666172616b61762e636f6d'
10
+ proxy_dc_id = 2 # This is usually 2 for MTProto proxies
11
 
12
+ #('hostname', port, 'secret')
13
+ proxy = (
14
+ socks.SOCKS5,
15
+ proxy_server,
16
+ proxy_port,
17
+ True,
18
+ 'vpn',
19
+ 'unlimited'
20
+ )
21
 
22
+ api_id = '10086982'
23
+ api_hash = '3ed461889a88b31fcbc323d13e43cf7e'
24
+ phone = '+963994935356'
 
 
 
 
 
 
 
 
25
 
26
+ # Dictionary to track the times when senders were last replied to
27
+ reply_times = {}
 
28
 
29
+ async with TelegramClient('anon', api_id, api_hash, proxy=proxy) as client:
30
+ @client.on(events.NewMessage())
31
+ async def my_event_handler(event):
32
+ sender = await event.get_sender()
33
+ sender_id = sender.id
34
+ sender_name = sender.first_name
35
+ chat = await event.get_chat()
36
+ chat_id = chat.id
37
+ text = event.raw_text
38
 
39
+ # Timekeeping
40
+ checkpoint = datetime.datetime.now()
41
+ print(f'Checkpoint: {checkpoint}')
42
 
43
+ # Personal message
44
+ if chat_id == sender_id:
45
+ # Check the last reply to this sender
46
+ last_reply_time = reply_times.get(sender_id, None)
47
+ if last_reply_time is None or time.time() - last_reply_time > 60*15: # reply only if not replied in the last minute
48
+ response = f'Hello {sender_name}, I received your message and will reply as soon as possible. Thank you for your understanding.'
49
+ await client.send_message(chat_id, response)
50
+ reply_times[sender_id] = time.time() # update the last reply time
51
 
52
+ # Group message
53
+ elif '@Mohammed_Alakhras' in text:
54
+ last_reply_time = reply_times.get(sender_id, None)
55
+ if last_reply_time is None or time.time() - last_reply_time > 60:
56
+ response = f'Hello {sender_name}, I received your message and will reply as soon as possible. Thank you for your understanding.'
57
+ await client.send_message(chat_id, response)
58
+ reply_times[sender_id] = time.time()
59
 
60
+ await client.run_until_disconnected()
61
  # ุชุนุฑูŠู ุญู‚ูˆู„ ุงู„ู†ุต ููŠ Gradio
62
 
63
  # ุชุนุฑูŠู ุงู„ูˆุงุฌู‡ุฉ