Spaces:
Sleeping
Sleeping
from flask import Flask, jsonify, render_template, request, send_file | |
from slack_sdk import WebClient | |
from slack_sdk.errors import SlackApiError | |
import os | |
from slack_bolt import App | |
import flask | |
import requests | |
import json | |
from simple_salesforce import Salesforce | |
import pandas as pd | |
app = flask.Flask(__name__, template_folder="static") | |
username=os.environ.get('SF_UserName') | |
password=os.environ.get('SF_password') | |
security_token=os.environ.get('security_token') | |
domain=os.environ.get('domain') | |
sf = Salesforce(username=username, password=password, security_token=security_token, domain=domain) | |
def search_knowledge(prompt): | |
slack_msg= "Here are knowledge articles related to "+prompt | |
try: | |
searchstring="Find {"+prompt+"}" | |
print(searchstring) | |
information = sf.search(searchstring) | |
df = pd.DataFrame(information['searchRecords']) | |
print(df) | |
for i in (df.index): | |
slack_msg=slack_msg+df['Id'][i] | |
except requests.exceptions.RequestException as e: | |
raise SystemExit(e) | |
pass | |
return slack_msg | |
def index(): | |
return 'Hello' | |
def handle_event(): | |
# Handle the event here | |
event=request.json['type'] | |
print("event is"+event) | |
print("request is"+ request.json['event'] ['text']) | |
if event=='url_verification': | |
# Respond to the challenge request with a 200 OK HTTP status code | |
# and the value of the challenge parameter in the response body | |
challenge = request.json['challenge'] | |
return ( | |
challenge | |
) | |
if event=='event_callback': | |
API_KEY=os.environ.get('SLACK_APP_TOKEN') | |
SLACK_CHANNEL=os.environ.get('SLACK_APP_CHANNEL') | |
# Set up the Slack client | |
YOUR_BOT_TOKEN=API_KEY | |
client = WebClient(token=YOUR_BOT_TOKEN) | |
channel= SLACK_CHANNEL | |
print(channel) | |
conversation = request.json['event'] ['text'] | |
print(conversation) | |
response=search_knowledge(conversation[15:]) | |
# Post the response back to the Slack channel | |
try: | |
client.chat_postMessage( | |
channel=channel, | |
text=response | |
) | |
except SlackApiError as e: | |
response="Error" | |
return response | |
if __name__ == "__main__": | |
app.run(host="0.0.0.0", port=7860) |