Tutorials
Build a slackbot to call your Berri / chatGPT endpoints
In this tutorial, we will go over how to build a custom Slackbot that can store mappings of channel IDs and API endpoints, and then call those API endpoints when requested.
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
In this tutorial, we will go over how to build a custom Slackbot that can store mappings of channel IDs and API endpoints, and then call those API endpoints when requested.
import logging
import os
from slack_bolt import App
from slack_bolt.oauth.oauth_settings import OAuthSettings
from slack_sdk.oauth.installation_store import FileInstallationStore
from slack_sdk.oauth.state_store import FileOAuthStateStore
from slack_bolt.adapter.flask import SlackRequestHandler
from slack_bolt.oauth import OAuthFlow
from slack_sdk import WebClient
import json
import requests
import uuid
oauth_settings = OAuthSettings(
client_id="YOUR_CLIENT_ID",
client_secret="YOUR_CLIENT_SECRET",
scopes=[
"chat:write", "app_mentions:read", "channels:join", "im:write",
"im:history", "chat:write.public", "commands", "mpim:write"
],
installation_store=FileInstallationStore(base_dir="./data/installations"),
state_store=FileOAuthStateStore(expiration_seconds=600,
base_dir="./data/states"),
redirect_uri="YOUR_REDIRECT_URI"
)
app = App(signing_secret="YOUR_SIGNING_SECRET",
oauth_settings=oauth_settings)
logging.basicConfig(level=logging.DEBUG)
client = WebClient(token="YOUR_BOT_TOKEN")
@app.event("message")
def handle_message():
pass
@app.event("app_mention")
def handle_mention(event, say):
print("got message from app")
thread_ts = event['thread_ts'] if 'thread_ts' in event else event['ts']
message_text = event["text"]
print(event)
channel_id = event["channel"]
thread_ts = event["ts"]
say("Hi from your Slackbot!")
response = requests.get("https://jsonplaceholder.typicode.com/todos/1")
if response.status_code == 200:
response = response.json()['title']
print(response)
else:
print(f"Request failed with status code {response.status_code}")
say(text=response, thread_ts=thread_ts)
flask_app = Flask(__name__)
handler = SlackRequestHandler(app)
@flask_app.route("/slack/events", methods=["POST"])
def slack_events():
return handler.handle(request)
@flask_app.route("/slack/install", methods=["GET"])
def install():
print(request.args)
return handler.handle(request)
@flask_app.route("/slack/oauth_redirect", methods=["GET"])
def oauth_redirect():
print("in flask oauth handler")
print(request.args)
return handler.handle(request)
