-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
265 lines (217 loc) · 7.98 KB
/
main.py
File metadata and controls
265 lines (217 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
"""
CometX Automation Bot - Main Entry Point
البوت الرئيسي مع الذاكرة الدائمة وواجهة التفاعل
"""
import asyncio
import os
from datetime import datetime
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from dotenv import load_dotenv
from database import init_db, SessionLocal, Conversation, Automation
from automation_engine import AutomationEngine
from connectors.github_connector import GitHubConnector
from connectors.azure_connector import AzureDevOpsConnector
from connectors.teams_connector import TeamsConnector
from connectors.canva_connector import CanvaConnector
load_dotenv()
# إنشاء قاعدة البيانات
init_db()
app = FastAPI(title="CometX Automation Bot")
# محرك الأتمتة
automation_engine = AutomationEngine()
class MessageRequest(BaseModel):
user_id: str = "default"
message: str
class AutomationRequest(BaseModel):
user_id: str = "default"
@app.get("/")
async def root():
"""معلومات البوت"""
return {
"bot_name": "CometX Automation Bot",
"version": "1.0.0",
"status": "active",
"features": [
"Persistent Memory",
"GitHub Integration",
"Azure DevOps Integration",
"HTTP Connector",
"Microsoft Teams",
"Canva Integration"
]
}
@app.post("/chat")
async def chat(request: MessageRequest):
"""
محادثة مع البوت - يحفظ كل شيء في الذاكرة
"""
db = SessionLocal()
try:
# جلب آخر 5 محادثات للسياق
recent_conversations = db.query(Conversation).filter(
Conversation.user_id == request.user_id
).order_by(Conversation.timestamp.desc()).limit(5).all()
context = "\n".join([
f"User: {conv.message}\nBot: {conv.response}"
for conv in reversed(recent_conversations)
])
# تحليل الرسالة
message_lower = request.message.lower()
if "start automation" in message_lower:
response = "🚀 جاري تشغيل عملية الأتمتة الكاملة..."
# حفظ المحادثة
conversation = Conversation(
user_id=request.user_id,
message=request.message,
response=response,
context=context
)
db.add(conversation)
db.commit()
# تشغيل الأتمتة
automation_result = await automation_engine.run_automation(request.user_id)
return {
"message": response,
"automation": automation_result,
"context": "triggered_automation"
}
elif "history" in message_lower or "تاريخ" in message_lower:
# عرض التاريخ
all_conversations = db.query(Conversation).filter(
Conversation.user_id == request.user_id
).order_by(Conversation.timestamp.desc()).limit(10).all()
history = [
{
"timestamp": conv.timestamp.isoformat(),
"message": conv.message,
"response": conv.response
}
for conv in all_conversations
]
response = f"📜 لديك {len(history)} محادثة محفوظة"
conversation = Conversation(
user_id=request.user_id,
message=request.message,
response=response,
context=context
)
db.add(conversation)
db.commit()
return {
"message": response,
"history": history
}
elif "status" in message_lower or "حالة" in message_lower:
# حالة آخر أتمتة
last_automation = db.query(Automation).filter(
Automation.user_id == request.user_id
).order_by(Automation.timestamp.desc()).first()
if last_automation:
response = f"""
📊 حالة آخر أتمتة:
- الحالة: {last_automation.status}
- الوقت: {last_automation.timestamp}
- GitHub: {last_automation.github_issue_url or 'N/A'}
- Azure: {last_automation.azure_pipeline_url or 'N/A'}
- Deploy: {last_automation.deploy_status or 'N/A'}
"""
else:
response = "لا توجد عمليات أتمتة سابقة"
conversation = Conversation(
user_id=request.user_id,
message=request.message,
response=response,
context=context
)
db.add(conversation)
db.commit()
return {"message": response}
else:
# رد عادي
response = f"""
مرحباً! أنا CometX Automation Bot 🤖
أقدر أساعدك في:
- قول "Start automation" لتشغيل الأتمتة الكاملة
- قول "history" لعرض تاريخ محادثاتنا
- قول "status" لمعرفة حالة آخر أتمتة
أنا أحفظ كل محادثاتنا في ذاكرتي الدائمة!
"""
conversation = Conversation(
user_id=request.user_id,
message=request.message,
response=response,
context=context
)
db.add(conversation)
db.commit()
return {"message": response}
finally:
db.close()
@app.post("/automation/trigger")
async def trigger_automation(request: AutomationRequest):
"""
تشغيل الأتمتة مباشرة عبر API
"""
result = await automation_engine.run_automation(request.user_id)
return result
@app.get("/automation/history/{user_id}")
async def get_automation_history(user_id: str):
"""
عرض تاريخ عمليات الأتمتة
"""
db = SessionLocal()
try:
automations = db.query(Automation).filter(
Automation.user_id == user_id
).order_by(Automation.timestamp.desc()).limit(20).all()
return {
"user_id": user_id,
"count": len(automations),
"automations": [
{
"id": auto.id,
"status": auto.status,
"timestamp": auto.timestamp.isoformat(),
"github_issue": auto.github_issue_url,
"azure_pipeline": auto.azure_pipeline_url,
"deploy_status": auto.deploy_status
}
for auto in automations
]
}
finally:
db.close()
@app.get("/memory/conversations/{user_id}")
async def get_conversations(user_id: str, limit: int = 20):
"""
عرض محادثات المستخدم من الذاكرة
"""
db = SessionLocal()
try:
conversations = db.query(Conversation).filter(
Conversation.user_id == user_id
).order_by(Conversation.timestamp.desc()).limit(limit).all()
return {
"user_id": user_id,
"count": len(conversations),
"conversations": [
{
"timestamp": conv.timestamp.isoformat(),
"message": conv.message,
"response": conv.response
}
for conv in conversations
]
}
finally:
db.close()
if __name__ == "__main__":
import uvicorn
print("=" * 60)
print("🤖 CometX Automation Bot Starting...")
print("=" * 60)
print(f"Bot Name: {os.getenv('BOT_NAME', 'CometX Automation Bot')}")
print(f"Trigger Phrase: {os.getenv('TRIGGER_PHRASE', 'Start automation')}")
print("=" * 60)
uvicorn.run(app, host="0.0.0.0", port=8000)