taslim19
commited on
Commit
Β·
14c5067
1
Parent(s):
3913e4e
Add Telegram bot functionality with command handlers
Browse files- OneApi/__init__.py +129 -0
- OneApi/__main__.py +8 -1
OneApi/__init__.py
CHANGED
@@ -28,6 +28,135 @@ if not API_ID or not API_HASH or not TOKEN or not MONGO_DB_URI:
|
|
28 |
# _-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_|
|
29 |
if len(TOKEN) > 50: bot = Client("OneApi", session_string=TOKEN, api_id=API_ID, api_hash=API_HASH, plugins=dict(root="OneApi/pyro"))
|
30 |
else: bot = Client("OneApi", bot_token=TOKEN, api_id=API_ID, api_hash=API_HASH, plugins=dict(root="OneApi/pyro"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
# ββββ R U N ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
32 |
async def run(command):
|
33 |
try:
|
|
|
28 |
# _-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_-_+_|
|
29 |
if len(TOKEN) > 50: bot = Client("OneApi", session_string=TOKEN, api_id=API_ID, api_hash=API_HASH, plugins=dict(root="OneApi/pyro"))
|
30 |
else: bot = Client("OneApi", bot_token=TOKEN, api_id=API_ID, api_hash=API_HASH, plugins=dict(root="OneApi/pyro"))
|
31 |
+
|
32 |
+
# Bot command handlers
|
33 |
+
@bot.on_message(filters.command("start"))
|
34 |
+
async def start_command(client, message):
|
35 |
+
user_id = message.from_user.id
|
36 |
+
user_name = message.from_user.first_name
|
37 |
+
|
38 |
+
welcome_text = f"""
|
39 |
+
π€ **Welcome to OneApi Cloud Hosting!**
|
40 |
+
|
41 |
+
Hi {user_name}! I'm your cloud hosting assistant.
|
42 |
+
|
43 |
+
**Available Commands:**
|
44 |
+
β’ `/start` - Show this help message
|
45 |
+
β’ `/info` - Get your account info
|
46 |
+
β’ `/projects` - List your projects
|
47 |
+
β’ `/create <name> <plan>` - Create new project
|
48 |
+
β’ `/connect` - Connect GitHub repository
|
49 |
+
|
50 |
+
**Plans Available:**
|
51 |
+
β’ Free - 0 coins
|
52 |
+
β’ Basic - 99 coins
|
53 |
+
β’ Advance - 199 coins
|
54 |
+
β’ Pro - 269 coins
|
55 |
+
|
56 |
+
**Your User ID:** `{user_id}`
|
57 |
+
|
58 |
+
Start by creating a project with `/create myapp free`
|
59 |
+
"""
|
60 |
+
|
61 |
+
await message.reply_text(welcome_text)
|
62 |
+
|
63 |
+
@bot.on_message(filters.command("info"))
|
64 |
+
async def info_command(client, message):
|
65 |
+
user_id = message.from_user.id
|
66 |
+
from .database import user
|
67 |
+
user_db = user()
|
68 |
+
|
69 |
+
user_info = await user_db.find(user_id)
|
70 |
+
if user_info:
|
71 |
+
info_text = f"""
|
72 |
+
π **Account Information**
|
73 |
+
|
74 |
+
**Name:** {user_info.get('name', 'Not set')}
|
75 |
+
**Coins:** {user_info.get('coins', 0)}
|
76 |
+
**Projects:** {len(user_info.get('projects', []))}
|
77 |
+
**GitHub:** {user_info.get('git', 'Not connected')}
|
78 |
+
**User ID:** {user_id}
|
79 |
+
"""
|
80 |
+
else:
|
81 |
+
info_text = f"""
|
82 |
+
β **User Not Found**
|
83 |
+
|
84 |
+
You need to register first. Use the API to create your account:
|
85 |
+
|
86 |
+
```bash
|
87 |
+
curl -X POST https://your-api-url/create_user/ \\
|
88 |
+
-H "Content-Type: application/json" \\
|
89 |
+
-d '{{"user_id": {user_id}, "name": "Your Name"}}'
|
90 |
+
```
|
91 |
+
|
92 |
+
**Your User ID:** `{user_id}`
|
93 |
+
"""
|
94 |
+
|
95 |
+
await message.reply_text(info_text)
|
96 |
+
|
97 |
+
@bot.on_message(filters.command("projects"))
|
98 |
+
async def projects_command(client, message):
|
99 |
+
user_id = message.from_user.id
|
100 |
+
from .database import user
|
101 |
+
user_db = user()
|
102 |
+
|
103 |
+
projects = await user_db.get_projects(user_id)
|
104 |
+
if projects == "not exists":
|
105 |
+
await message.reply_text("β User not found. Please register first.")
|
106 |
+
return
|
107 |
+
elif projects == "projects not found":
|
108 |
+
await message.reply_text("π No projects found. Create one with `/create myapp free`")
|
109 |
+
return
|
110 |
+
|
111 |
+
projects_text = "π **Your Projects:**\n\n"
|
112 |
+
for i, project in enumerate(projects, 1):
|
113 |
+
projects_text += f"{i}. **{project.get('name')}** (ID: {project.get('project_id')})\n"
|
114 |
+
|
115 |
+
await message.reply_text(projects_text)
|
116 |
+
|
117 |
+
@bot.on_message(filters.command("create"))
|
118 |
+
async def create_project_command(client, message):
|
119 |
+
user_id = message.from_user.id
|
120 |
+
from .database import user
|
121 |
+
user_db = user()
|
122 |
+
|
123 |
+
try:
|
124 |
+
args = message.text.split()[1:]
|
125 |
+
if len(args) < 2:
|
126 |
+
await message.reply_text("β Usage: `/create <name> <plan>`\n\nExample: `/create myapp free`")
|
127 |
+
return
|
128 |
+
|
129 |
+
name = args[0]
|
130 |
+
plan = args[1]
|
131 |
+
|
132 |
+
result = await user_db.create_project(name, user_id, plan)
|
133 |
+
|
134 |
+
if result == "ok":
|
135 |
+
await message.reply_text(f"β
Project '{name}' created successfully with {plan} plan!")
|
136 |
+
elif result == "not exists":
|
137 |
+
await message.reply_text("β User not found. Please register first.")
|
138 |
+
elif result == "Name too short":
|
139 |
+
await message.reply_text("β Project name must be at least 4 characters long.")
|
140 |
+
elif result == "Name already used":
|
141 |
+
await message.reply_text("β A project with this name already exists.")
|
142 |
+
elif result == "Plan not found":
|
143 |
+
await message.reply_text("β Invalid plan. Available: free, basic, advance, pro")
|
144 |
+
elif result == "insufficient coins":
|
145 |
+
await message.reply_text("β Insufficient coins for this plan.")
|
146 |
+
else:
|
147 |
+
await message.reply_text(f"β Error: {result}")
|
148 |
+
|
149 |
+
except Exception as e:
|
150 |
+
await message.reply_text(f"β Error creating project: {str(e)}")
|
151 |
+
|
152 |
+
# Start the bot
|
153 |
+
async def start_bot():
|
154 |
+
try:
|
155 |
+
await bot.start()
|
156 |
+
logging.info("Telegram bot started successfully!")
|
157 |
+
except Exception as e:
|
158 |
+
logging.error(f"Failed to start bot: {e}")
|
159 |
+
|
160 |
# ββββ R U N ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
161 |
async def run(command):
|
162 |
try:
|
OneApi/__main__.py
CHANGED
@@ -38,5 +38,12 @@ app = cors(app, allow_origin="*")
|
|
38 |
async def home():
|
39 |
return jsonify({'success': 'server online'}), 200
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
if __name__ == '__main__':
|
42 |
-
|
|
|
38 |
async def home():
|
39 |
return jsonify({'success': 'server online'}), 200
|
40 |
|
41 |
+
async def main():
|
42 |
+
# Start the bot
|
43 |
+
await start_bot()
|
44 |
+
|
45 |
+
# Start the web server
|
46 |
+
await app.run_task(host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
|
47 |
+
|
48 |
if __name__ == '__main__':
|
49 |
+
asyncio.run(main())
|