-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
61 lines (51 loc) · 1.85 KB
/
bot.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
import os
from slack_bolt.adapter.flask import SlackRequestHandler
from slack_bolt import App
from dotenv import find_dotenv, load_dotenv
from flask import Flask, request
from agent import genius
from langchain.embeddings import CohereEmbeddings
from langchain.vectorstores import Chroma
# Load environment variables from .env file
load_dotenv(find_dotenv('private/.env'))
# Set Slack API credentials
SLACK_BOT_TOKEN = os.environ["SLACK_BOT_TOKEN"]
SLACK_SIGNING_SECRET = os.environ["SLACK_SIGNING_SECRET"]
SLACK_BOT_USER_ID = os.environ["SLACK_BOT_USER_ID"]
COHERE_API_KEY = os.environ["COHERE_API_KEY"]
# Initialize the Slack app
app = App(token=SLACK_BOT_TOKEN)
# Initialize the Flask app
flask_app = Flask(__name__)
handler = SlackRequestHandler(app)
# Set up your base vector base
# Get Cohere embedding model (you may also use other models)
embeddings = CohereEmbeddings(cohere_api_key=COHERE_API_KEY)
text = "INSERT YOUR TEXT HERE"
db = Chroma.from_texts(text, embeddings)
@app.event("app_mention")
def handle_mentions(body, say):
"""
Event listener for mentions in Slack.
This function send a response to the event when the bot is mentioned.
body (dict): event data
say (callable): function for sending a response
"""
# Get query from event (i.e. user's message)
query = body["event"]["text"]
mention = f"<@{SLACK_BOT_USER_ID}>"
query = query.replace(mention, "").strip()
# Response
say('One moment please...')
ans = genius(query, db) # genius is the engine of the bot!
say(ans)
@flask_app.route("/slack/events", methods=["POST"])
def slack_events():
"""
Route for handling Slack events.
This function passes the incoming HTTP request to the SlackRequestHandler.
"""
return handler.handle(request) # result of handling the request
# Run the Flask app
if __name__ == "__main__":
flask_app.run()