import os
import re

from dotenv import load_dotenv
from langchain.chat_models import ChatOpenAI
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler

load_dotenv()

# ボットトークンを使ってアプリを初期化します
app = App(token=os.environ.get("SLACK_BOT_TOKEN"))


@app.event("app_mention")
def handle_mention(event, say):
    thread_ts = event["ts"]
    message = re.sub("<@.*>", "", event["text"])

    llm = ChatOpenAI(
        model_name=os.environ["OPENAI_API_MODEL"],
        temperature=os.environ["OPENAI_API_TEMPERATURE"],
    )

    response = llm.predict(message)
    say(text=response, thread_ts=thread_ts)


# ソケットモードハンドラーを使ってアプリを起動します
if __name__ == "__main__":
    SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start()

langchain.chat_models.ChatOpenAI
[((186, 199), 'dotenv.load_dotenv', 'load_dotenv', ([], {}), '()\n', (197, 199), False, 'from dotenv import load_dotenv\n'), ((378, 412), 're.sub', 're.sub', (['"""<@.*>"""', '""""""', "event['text']"], {}), "('<@.*>', '', event['text'])\n", (384, 412), False, 'import re\n'), ((424, 532), 'langchain.chat_models.ChatOpenAI', 'ChatOpenAI', ([], {'model_name': "os.environ['OPENAI_API_MODEL']", 'temperature': "os.environ['OPENAI_API_TEMPERATURE']"}), "(model_name=os.environ['OPENAI_API_MODEL'], temperature=os.\n    environ['OPENAI_API_TEMPERATURE'])\n", (434, 532), False, 'from langchain.chat_models import ChatOpenAI\n'), ((241, 274), 'os.environ.get', 'os.environ.get', (['"""SLACK_BOT_TOKEN"""'], {}), "('SLACK_BOT_TOKEN')\n", (255, 274), False, 'import os\n'), ((693, 746), 'slack_bolt.adapter.socket_mode.SocketModeHandler', 'SocketModeHandler', (['app', "os.environ['SLACK_APP_TOKEN']"], {}), "(app, os.environ['SLACK_APP_TOKEN'])\n", (710, 746), False, 'from slack_bolt.adapter.socket_mode import SocketModeHandler\n')]