-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilot_studio_client.py
More file actions
128 lines (105 loc) · 4.2 KB
/
copilot_studio_client.py
File metadata and controls
128 lines (105 loc) · 4.2 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
"""
Copilot Studio Bot Client
اتصال مع Copilot Studio Bot عبر API
"""
import os
import httpx
import asyncio
from dotenv import load_dotenv
from datetime import datetime
load_dotenv()
class CopilotStudioClient:
def __init__(self):
self.api_url = os.getenv("COPILOT_STUDIO_API_URL")
self.token = os.getenv("COPILOT_STUDIO_TOKEN")
if not self.api_url:
print("⚠️ COPILOT_STUDIO_API_URL مو موجود في .env")
else:
print(f"✅ Bot URL: {self.api_url[:50]}...")
async def send_message(self, message, user_id="test_user"):
"""إرسال رسالة للبوت"""
try:
headers = {
"Content-Type": "application/json"
}
# إضافة التوكن إذا موجود وليس فارغ
if self.token and self.token.strip():
headers["Authorization"] = f"Bearer {self.token}"
print("🔑 استخدام التوكن للمصادقة")
else:
print("⚠️ محاولة الاتصال بدون توكن")
payload = {
"text": message,
"locale": "en-US",
"channelId": "directline",
"from": {
"id": user_id,
"name": "Test User"
}
}
print(f"\n📤 إرسال رسالة: {message}")
print(f"🔗 إلى: {self.api_url}")
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.post(
self.api_url,
json=payload,
headers=headers
)
print(f"📊 Status Code: {response.status_code}")
if response.status_code in [200, 201, 202]:
result = response.json()
print(f"✅ رد البوت:")
print(result)
return {
"success": True,
"response": result
}
else:
print(f"❌ خطأ: {response.status_code}")
print(f"📝 التفاصيل: {response.text}")
return {
"success": False,
"error": response.text,
"status_code": response.status_code
}
except Exception as e:
print(f"❌ خطأ في الاتصال: {e}")
return {
"success": False,
"error": str(e)
}
async def test_connection(self):
"""اختبار الاتصال بالبوت"""
print("=" * 60)
print("🤖 اختبار الاتصال بـ Copilot Studio Bot")
print("=" * 60)
# تجربة رسالة بسيطة
result = await self.send_message("Hello! Are you there?")
if result.get("success"):
print("\n✅ الاتصال نجح!")
# تجربة أوامر أخرى
print("\n" + "=" * 60)
print("📋 تجربة الأوامر:")
print("=" * 60)
commands = [
"Show me my GitHub repositories",
"List Azure DevOps pipelines",
"What can you do?"
]
for cmd in commands:
print(f"\n🔸 الأمر: {cmd}")
await asyncio.sleep(2) # انتظار ثانيتين
await self.send_message(cmd)
else:
print("\n❌ فشل الاتصال!")
print("🔧 تأكد من:")
print(" 1. URL صحيح في .env")
print(" 2. التوكن صحيح (إذا مطلوب)")
print(" 3. البوت منشور ومفعل")
async def main():
"""الدالة الرئيسية"""
client = CopilotStudioClient()
await client.test_connection()
if __name__ == "__main__":
print("🚀 بدء اختبار Copilot Studio Bot...")
asyncio.run(main())