-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
128 lines (114 loc) · 6.22 KB
/
app.py
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
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
from flask import Flask, request, jsonify
import os
from dotenv import load_dotenv
import requests
# Load environment variables
load_dotenv()
app = Flask(__name__)
# Set bot token (from .env file)
SLACK_BOT_TOKEN = os.getenv("SLACK_BOT_TOKEN")
# Slack client
client = WebClient(token=SLACK_BOT_TOKEN)
@app.route("/slack/events", methods=["POST"])
def slack_events():
data = request.json
print("Received data:", data)
if "challenge" in data:
return jsonify({"challenge": data["challenge"]})
if "event" in data:
event = data["event"]
print(f"Event: {event}")
if event.get("type") == "message" and "bot_id" not in event:
channel = event["channel"]
user = event["user"]
text = event["text"].lower()
if text[0] == '!':
try:
remainder = text[0:]
print(remainder)
url = "http://138.197.102.64:6000/ask" # Flask server URL
params = {"query": remainder} # Attach query as a parameter
response = requests.get(url, params=params)
if response.status_code == 200:
out = response.json()
out = out['answer']
print(response.json()) # Parse JSON response
else:
# return print({"error": f"Request failed with status code {response.status_code}"})
out = {"error": f"Request failed with status code {response.status_code}"}
client.chat_postMessage(
channel=channel, text=f"{out}") # change text to the output of the model
except SlackApiError as e:
print(f"Error: {e.response['error']}")
else:
try:
client.chat_postMessage(
channel=channel, text=f"Hello <@{user}>, please use an ! as a prefix to your question")
except SlackApiError as e:
print(f"Error: {e.response['error']}")
# if text == "hello":
# try:
# client.chat_postMessage(
# channel=channel, text=f"Hello <@{user}>")
# except SlackApiError as e:
# print(f"Error: {e.response['error']}")
# elif text[0] == "!" and "generate’s mission" in text:
# try:
# client.chat_postMessage(
# channel=channel, text=f"Generate is a community of passionate individuals who engage in collaborative, real-world experiences to develop innovative products, the organization, its partners, and themselves.")
# except SlackApiError as e:
# print(f"Error: {e.response['error']}")
# elif text[0] == "!" and "generate’s values" in text:
# try:
# client.chat_postMessage(
# channel=channel, text="""
# Generate is under the Sherman Center, whose values must align with three core values—being developmental, inclusive, and intentional.
# In addition, Generate members are innovative, driven, empathetic, spirited, and growth-oriented.
# """)
# except SlackApiError as e:
# print(f"Error: {e.response['error']}")
# elif text[0] == "!" and "access isn’t working" in text:
# try:
# client.chat_postMessage(
# channel=channel, text="""
# If you notice a keycard reader isn’t working, please let someone know so we can diagnose the issue and quickly resolve it. A few things to try to help escalate:
# 1. **Understand if it’s an issue with your access, or with everyone’s access:**
# - If it’s an issue with your access, please log into the Generate Ops Dashboard to check if you have been added to the access list for the spaces. This issue is common at the start of a semester.
# - If the Dashboard says you have access, but you are experiencing an issue that no one else is, please send a message in #ops-help on Slack. Be sure to mention the space you are trying to access and what behavior you are seeing.
# 2. **If others also have the issue, and the keycard reader is not flashing red or green or making a beep when reading a card, it’s likely the reader is out of battery. In this case:**
# - Please send a message in #ops-help on Slack.
# - Additionally, please submit a work request here:
# - Problem Type: Locks, Keys and Card Access
# - Campus: Boston
# - Building name: Hayden
# - Floor: 0 | Basement
# - Room number: 008 | Private Circulation Area
# - Location within room: Front door
# - Description: Door keycard reader requires AD400 battery change.
# """)
# except SlackApiError as e:
# print(f"Error: {e.response['error']}")
# elif text[0] == "!" and "ryder and cooper coming" in text:
# try:
# client.chat_postMessage(
# channel=channel, text="Ryder and Cooper will visit the Sherman Center on February 14, 2025, from 1:30 PM - 2:00 PM.")
# except SlackApiError as e:
# print(f"Error: {e.response['error']}")
# elif text[0] == "!" and "all hands" in text:
# try:
# client.chat_postMessage(
# channel=channel, text="All Hands is on February 20, 2025, from 7:00 PM - 9:00 PM.")
# except SlackApiError as e:
# print(f"Error: {e.response['error']}")
# elif text[0] == "!" and "showcase" in text:
# try:
# client.chat_postMessage(
# channel=channel, text="Showcase is on April 11, 2025, from 6:30 PM - 9:30 PM in ISEC 102.")
# except SlackApiError as e:
# print(f"Error: {e.response['error']}")
return jsonify({"status": "ok"}), 200
if __name__ == "__main__":
from os import getenv
app.run(debug=True, host="0.0.0.0", port=3000)