Telegram_API / app.py
MohammedAlakhras's picture
Update app.py
3e96bbe
raw
history blame
1.69 kB
import gradio as gr
from telethon.sync import TelegramClient, events
import asyncio
import datetime
api_id = '10086982'
api_hash = '3ed461889a88b31fcbc323d13e43cf7e'
def handle_messages():
async def process_messages():
async with TelegramClient('anon', api_id, api_hash) as client:
@client.on(events.NewMessage())
async def my_event_handler(event):
sender = await event.get_sender()
sender_id = sender.id
sender_name = sender.first_name
chat = await event.get_chat()
chat_id = chat.id
text = event.raw_text
# ู…ู‚ูŠุงุณ ุงู„ุฒู…ู†
checkpoint = datetime.datetime.now()
print(f'Checkpoint: {checkpoint}')
# ุฑุณุงู„ุฉ ุดุฎุตูŠุฉ
if chat_id == sender_id:
response = f'ุงู‡ู„ุง {sender_name} , ู„ู‚ุฏ ุชู„ู‚ูŠุช ุฑุณุงู„ุชูƒ ุณุฃู‚ูˆู… ุจุงู„ุฑุฏ ุนู„ูŠูƒ ุจุฃู‚ุฑุจ ูˆู‚ุช ู…ู…ูƒู† ูˆุดูƒุฑุงู‹ ู„ุชูู‡ู…ูƒ.'
await client.send_message(chat_id, response)
# ุฑุณุงู„ุฉ ู…ุฌู…ูˆุนุฉ
elif '@Mohammed_Alakhras' in text:
response = f'ุงู‡ู„ุง {sender_name} , ู„ู‚ุฏ ุชู„ู‚ูŠุช ุฑุณุงู„ุชูƒ ุณุฃู‚ูˆู… ุจุงู„ุฑุฏ ุนู„ูŠูƒ ุจุฃู‚ุฑุจ ูˆู‚ุช ู…ู…ูƒู† ูˆุดูƒุฑุงู‹ ู„ุชูู‡ู…ูƒ.'
await client.send_message(chat_id, response)
await client.run_until_disconnected()
asyncio.run(process_messages())
# ุชุนุฑูŠู ุญู‚ูˆู„ ุงู„ู†ุต ููŠ Gradio
# ุชุนุฑูŠู ุงู„ูˆุงุฌู‡ุฉ
inputs = []
output = "text"
gr.Interface(fn=handle_messages, inputs=inputs, outputs=output).launch()