Spaces:
Runtime error
Runtime error
92abdelrahman
commited on
Commit
β’
debafc1
1
Parent(s):
f13a65c
Add Telechat files
Browse files- Dockerfile +23 -0
- main.py +124 -0
- requirements.txt +5 -0
Dockerfile
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use an official Python runtime as a parent image
|
2 |
+
FROM python:3.9
|
3 |
+
|
4 |
+
# Set the working directory in the container
|
5 |
+
WORKDIR /app
|
6 |
+
|
7 |
+
# Copy the current directory contents into the container at /app
|
8 |
+
COPY . /app
|
9 |
+
|
10 |
+
# Install any needed packages specified in requirements.txt
|
11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
12 |
+
|
13 |
+
# Set environment variables, including TRANSFORMERS_CACHE
|
14 |
+
ENV TRANSFORMERS_CACHE /app/cache
|
15 |
+
|
16 |
+
# Set cache directory permissions
|
17 |
+
RUN mkdir -p /app/cache && chmod 777 /app/cache
|
18 |
+
|
19 |
+
# Make port 7860 available to the world outside this container
|
20 |
+
EXPOSE 7860
|
21 |
+
|
22 |
+
# Define the command to run your application
|
23 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from fastapi import Form, Depends, Body
|
3 |
+
from concurrent.futures import ThreadPoolExecutor
|
4 |
+
from telethon.tl.functions.messages import ExportChatInviteRequest
|
5 |
+
from telethon.sessions import StringSession
|
6 |
+
|
7 |
+
import asyncio
|
8 |
+
from telethon.sync import TelegramClient
|
9 |
+
from telethon.tl import functions
|
10 |
+
import requests, json, os
|
11 |
+
|
12 |
+
app = FastAPI()
|
13 |
+
|
14 |
+
api_id = os.environ.get('api_id')
|
15 |
+
api_hash = os.environ.get('api_hash')
|
16 |
+
phone_number = os.environ.get('phone_number')
|
17 |
+
|
18 |
+
user_admin = os.environ.get('user_admin')
|
19 |
+
user_creator = os.environ.get('user_creator')
|
20 |
+
user_mentor = os.environ.get('user_mentor')
|
21 |
+
admin_notifier_chat_id = os.environ.get('admin_notifier_chat_id')
|
22 |
+
|
23 |
+
current_session_string = "1BJWap1sBu43IhVJj9siFBcuC8MYtjC7ebqcCnnTTytu_HM7QWoYFOe38xjlvDhJyF2vtuJDblEvSzXkoe31EIjTLdj23m0oDzNxmQJt0_J-UI7fcTpbJSeppcdxuCK4-dU3fQ2Ap_51I0NljF1TpQ6NrO6JArLOMXE9PVdvFiGRKvMM78_zfFGccPUt3fSwPidFdTNBy1bbTFzYI7nviB1jf3WeSnHzN3ORuSs_uMmOXTH12qI92mFto1-E-4Ct8MprM2lwqPO-hosyrRaA8sBTec8AQanjFK0ft5SbXpCpJYV8dFdsufiQrqnysCa4PAJ9Hf305FlC3BVnl5yUtXX6sG1pxsaU="
|
24 |
+
|
25 |
+
# Create a new Telegram client
|
26 |
+
client = TelegramClient(StringSession(current_session_string), api_id, api_hash)
|
27 |
+
|
28 |
+
register_new_chat_google_apps_script_url = os.environ.get('script_url')
|
29 |
+
|
30 |
+
def register_new_chat(group_title, chat_id, start_date, instructor_name, session_days, invite_link):
|
31 |
+
response = requests.get(register_new_chat_google_apps_script_url, params={"newChatCreated": True, "chatTitle": group_title, "chatID": chat_id, "startDate": start_date, "instructorName": instructor_name, "sessionDays": session_days, "inviteLink": invite_link})
|
32 |
+
data = json.loads(response.text)
|
33 |
+
print(f"reponse from the API is {data}")
|
34 |
+
return data
|
35 |
+
|
36 |
+
async def create_group(group_title, event_type, start_date, duration, recurrence_days, recurrence, calendar_id, instructor_name):
|
37 |
+
print("Now creating group..")
|
38 |
+
# await client.start(phone_number)
|
39 |
+
print("Client started..")
|
40 |
+
try:
|
41 |
+
# Create a group
|
42 |
+
result = await client(functions.messages.CreateChatRequest(
|
43 |
+
users=[user_admin],
|
44 |
+
title=group_title
|
45 |
+
))
|
46 |
+
|
47 |
+
# Get the created chat ID
|
48 |
+
chat_id = result.chats[0].id
|
49 |
+
# Get the group invite link
|
50 |
+
invite = await client(ExportChatInviteRequest(chat_id))
|
51 |
+
invite_link = invite.link
|
52 |
+
print(f'Group invite link: {invite_link}')
|
53 |
+
|
54 |
+
print(f'Group created successfully! Chat ID: {chat_id}')
|
55 |
+
|
56 |
+
# Additional processing with the new parameters
|
57 |
+
print(f'Event Type: {event_type}')
|
58 |
+
print(f'Start Date: {start_date}')
|
59 |
+
print(f'Duration: {duration}')
|
60 |
+
print(f'Recurrence Days: {recurrence_days}')
|
61 |
+
print(f'Recurrence: {recurrence}')
|
62 |
+
print(f'Calendar ID: {calendar_id}')
|
63 |
+
print(f'Instructor name: {instructor_name}')
|
64 |
+
|
65 |
+
register_new_chat_result = register_new_chat(group_title, chat_id, start_date, instructor_name, recurrence_days, invite_link)
|
66 |
+
|
67 |
+
# Leave the group
|
68 |
+
await client(functions.messages.DeleteChatUserRequest(
|
69 |
+
chat_id=chat_id,
|
70 |
+
user_id='me',
|
71 |
+
))
|
72 |
+
|
73 |
+
await client.delete_dialog(chat_id)
|
74 |
+
print('Left the group successfully!')
|
75 |
+
print("Status: ", register_new_chat_result)
|
76 |
+
|
77 |
+
if register_new_chat_result['Status'] == 'Chat details was added sucessfully to the repo':
|
78 |
+
# message = f"π *New Event was created successfully:*\nπ‘ *Event/Chat title:* {group_title}\nπ *Start Date:* {start_date}\nπ *Invite link:* {invite_link}\nπ©βπ« *Instructor:* {instructor_name}\nπ *Session days:* {recurrence_days}"
|
79 |
+
message = (
|
80 |
+
f"π <b>New Event was created successfully:</b>\n"
|
81 |
+
f"π‘ <b>Event/Chat title:</b> {group_title}\n"
|
82 |
+
f"π <b>Start Date:</b> {start_date}\n"
|
83 |
+
f"π <b>Invite link:</b> {invite_link}\n"
|
84 |
+
f"π©βπ« <b>Instructor:</b> {instructor_name}\n"
|
85 |
+
f"π <b>Session days:</b> {recurrence_days}"
|
86 |
+
)
|
87 |
+
|
88 |
+
# Send the invite link to the group chat
|
89 |
+
await client.send_message(
|
90 |
+
entity=admin_notifier_chat_id,
|
91 |
+
message=message,
|
92 |
+
parse_mode='html'
|
93 |
+
)
|
94 |
+
except Exception as e:
|
95 |
+
print(f"Error creating/leaving the group: {e}")
|
96 |
+
|
97 |
+
finally:
|
98 |
+
# Close the client connection
|
99 |
+
await client.disconnect()
|
100 |
+
|
101 |
+
@app.post("/createGroups")
|
102 |
+
async def create_groups(
|
103 |
+
group_title: str = Form(...),
|
104 |
+
event_type: str = Form(...),
|
105 |
+
start_date: str = Form(...),
|
106 |
+
duration: str = Form(...),
|
107 |
+
recurrence_days: str = Form(...),
|
108 |
+
recurrence: str = Form(...),
|
109 |
+
calendar_id: str = Form(...),
|
110 |
+
instructor_name: str = Form(...)
|
111 |
+
):
|
112 |
+
print("Received a request")
|
113 |
+
|
114 |
+
loop = asyncio.get_event_loop()
|
115 |
+
with ThreadPoolExecutor() as executor:
|
116 |
+
await loop.run_in_executor(
|
117 |
+
executor,
|
118 |
+
lambda: asyncio.run_coroutine_threadsafe(
|
119 |
+
create_group(group_title, event_type, start_date, duration, recurrence_days, recurrence, calendar_id, instructor_name),
|
120 |
+
loop
|
121 |
+
)
|
122 |
+
)
|
123 |
+
|
124 |
+
return {"status": "Supergroup creation initiated"}
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
uvicorn
|
2 |
+
telethon
|
3 |
+
python-multipart
|
4 |
+
requests
|
5 |
+
fastapi
|