Wajahat698 commited on
Commit
948beff
·
verified ·
1 Parent(s): d1d5704

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -11
app.py CHANGED
@@ -1,4 +1,7 @@
1
  from flask import Flask, request, jsonify
 
 
 
2
  import logging
3
 
4
  app = Flask(__name__)
@@ -6,29 +9,88 @@ app = Flask(__name__)
6
  logging.basicConfig(level=logging.INFO)
7
  logger = logging.getLogger(__name__)
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  @app.route("/")
10
  def home():
11
- return "Slack ConnectWise Bot Running"
12
 
13
- @app.route("/slack/events", methods=["GET", "POST"])
14
- def slack_events():
15
- logger.info("Incoming request: %s %s", request.method, request.path)
16
 
17
- if request.method == "GET":
18
- return "Slack endpoint alive", 200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- data = request.get_json(silent=True) or {}
 
 
 
 
 
 
21
  logger.info("Slack payload: %s", data)
22
 
23
- # Slack URL verification
24
  if data.get("type") == "url_verification":
25
  return jsonify({"challenge": data.get("challenge")})
26
 
27
  event = data.get("event", {})
28
- logger.info("Event: %s", event)
29
 
30
- if event.get("type") == "message" and not event.get("bot_id"):
 
 
 
 
 
31
  text = event.get("text")
32
- logger.info("User message: %s", text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  return "", 200
 
1
  from flask import Flask, request, jsonify
2
+ import requests
3
+ import base64
4
+ import os
5
  import logging
6
 
7
  app = Flask(__name__)
 
9
  logging.basicConfig(level=logging.INFO)
10
  logger = logging.getLogger(__name__)
11
 
12
+ # Environment variables (set these in Hugging Face Secrets)
13
+ SLACK_TOKEN ="xoxb-7025747032292-10683671864694-lhUNPKF1KSpYwRvrvZeP7CPy"
14
+ CW_COMPANY_ID = "Intrinsic"
15
+ CW_PUBLIC_KEY = "fE9y4GPRto0GaIcK"
16
+ CW_PRIVATE_KEY = "pns6fJBygEiD08Vx"
17
+ CW_CLIENT_ID = "e45cf35f-24a8-4f5f-b47a-19376cafce64"
18
+
19
+ BASE_URL = "https://api-na.myconnectwise.net/v4_6_release/apis/3.0"
20
+
21
+ auth = f"{CW_COMPANY_ID}+{CW_PUBLIC_KEY}:{CW_PRIVATE_KEY}"
22
+ encoded_auth = base64.b64encode(auth.encode()).decode()
23
+
24
+ cw_headers = {
25
+ "Authorization": f"Basic {encoded_auth}",
26
+ "ClientID": CW_CLIENT_ID,
27
+ "Content-Type": "application/json"
28
+ }
29
+
30
+
31
  @app.route("/")
32
  def home():
33
+ return "Slack ConnectWise Bot Running"
34
 
 
 
 
35
 
36
+ def create_ticket(issue):
37
+
38
+ ticket = {
39
+ "summary": issue[:100],
40
+ "initialDescription": issue,
41
+ "board": {"id": 1},
42
+ "company": {"id": 1},
43
+ "priority": {"id": 2}
44
+ }
45
+
46
+ r = requests.post(
47
+ f"{BASE_URL}/service/tickets",
48
+ headers=cw_headers,
49
+ json=ticket
50
+ )
51
+
52
+ if r.status_code in [200, 201]:
53
+ return r.json().get("id")
54
 
55
+ return "error"
56
+
57
+
58
+ @app.route("/slack/events", methods=["POST"])
59
+ def slack_events():
60
+
61
+ data = request.get_json()
62
  logger.info("Slack payload: %s", data)
63
 
 
64
  if data.get("type") == "url_verification":
65
  return jsonify({"challenge": data.get("challenge")})
66
 
67
  event = data.get("event", {})
 
68
 
69
+ # Ignore bot messages
70
+ if event.get("bot_id"):
71
+ return "", 200
72
+
73
+ if event.get("type") == "message":
74
+
75
  text = event.get("text")
76
+ channel = event.get("channel")
77
+
78
+ # Remove bot mention
79
+ text = text.replace("<@U0AL3KRRELE>", "").strip()
80
+
81
+ ticket_id = create_ticket(text)
82
+
83
+ # Send reply to Slack
84
+ requests.post(
85
+ "https://slack.com/api/chat.postMessage",
86
+ headers={
87
+ "Authorization": f"Bearer {SLACK_TOKEN}",
88
+ "Content-Type": "application/json"
89
+ },
90
+ json={
91
+ "channel": channel,
92
+ "text": f"🎫 Support ticket created\nTicket ID: {ticket_id}"
93
+ }
94
+ )
95
 
96
  return "", 200