initial commit

This commit is contained in:
2026-05-11 12:36:20 +05:30
commit 384cbe8019
15377 changed files with 2360544 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
"""Slack tools."""
from langchain_community.tools.slack.get_channel import SlackGetChannel
from langchain_community.tools.slack.get_message import SlackGetMessage
from langchain_community.tools.slack.schedule_message import SlackScheduleMessage
from langchain_community.tools.slack.send_message import SlackSendMessage
from langchain_community.tools.slack.utils import login
__all__ = [
"SlackGetChannel",
"SlackGetMessage",
"SlackScheduleMessage",
"SlackSendMessage",
"login",
]

View File

@@ -0,0 +1,27 @@
"""Base class for Slack tools."""
from __future__ import annotations
from typing import TYPE_CHECKING
from langchain_core.tools import BaseTool
from pydantic import Field
from langchain_community.tools.slack.utils import login
if TYPE_CHECKING:
# This is for linting and IDE typehints
from slack_sdk import WebClient
else:
try:
# We do this so pydantic can resolve the types when instantiating
from slack_sdk import WebClient
except ImportError:
pass
class SlackBaseTool(BaseTool):
"""Base class for Slack tools."""
client: WebClient = Field(default_factory=login)
"""The WebClient object."""

View File

@@ -0,0 +1,37 @@
import json
import logging
from typing import Any, Optional
from langchain_core.callbacks import CallbackManagerForToolRun
from langchain_community.tools.slack.base import SlackBaseTool
class SlackGetChannel(SlackBaseTool):
"""Tool that gets Slack channel information."""
name: str = "get_channelid_name_dict"
description: str = (
"Use this tool to get channelid-name dict. There is no input to this tool"
)
def _run(
self, *args: Any, run_manager: Optional[CallbackManagerForToolRun] = None
) -> str:
try:
logging.getLogger(__name__)
result = self.client.conversations_list()
channels = result["channels"]
filtered_result = [
{key: channel[key] for key in ("id", "name", "created", "num_members")}
for channel in channels
if "id" in channel
and "name" in channel
and "created" in channel
and "num_members" in channel
]
return json.dumps(filtered_result, ensure_ascii=False)
except Exception as e:
return "Error creating conversation: {}".format(e)

View File

@@ -0,0 +1,44 @@
import json
import logging
from typing import Optional, Type
from langchain_core.callbacks import CallbackManagerForToolRun
from pydantic import BaseModel, Field
from langchain_community.tools.slack.base import SlackBaseTool
class SlackGetMessageSchema(BaseModel):
"""Input schema for SlackGetMessages."""
channel_id: str = Field(
...,
description="The channel id, private group, or IM channel to send message to.",
)
class SlackGetMessage(SlackBaseTool):
"""Tool that gets Slack messages."""
name: str = "get_messages"
description: str = "Use this tool to get messages from a channel."
args_schema: Type[SlackGetMessageSchema] = SlackGetMessageSchema
def _run(
self,
channel_id: str,
run_manager: Optional[CallbackManagerForToolRun] = None,
) -> str:
logging.getLogger(__name__)
try:
result = self.client.conversations_history(channel=channel_id)
messages = result["messages"]
filtered_messages = [
{key: message[key] for key in ("user", "text", "ts")}
for message in messages
if "user" in message and "text" in message and "ts" in message
]
return json.dumps(filtered_messages, ensure_ascii=False)
except Exception as e:
return "Error creating conversation: {}".format(e)

View File

@@ -0,0 +1,60 @@
import logging
from datetime import datetime as dt
from typing import Optional, Type
from langchain_core.callbacks import CallbackManagerForToolRun
from pydantic import BaseModel, Field
from langchain_community.tools.slack.base import SlackBaseTool
from langchain_community.tools.slack.utils import UTC_FORMAT
logger = logging.getLogger(__name__)
class ScheduleMessageSchema(BaseModel):
"""Input for ScheduleMessageTool."""
message: str = Field(
...,
description="The message to be sent.",
)
channel: str = Field(
...,
description="The channel, private group, or IM channel to send message to.",
)
timestamp: str = Field(
...,
description="The datetime for when the message should be sent in the "
' following format: YYYY-MM-DDTHH:MM:SS±hh:mm, where "T" separates the date '
" and time components, and the time zone offset is specified as ±hh:mm. "
' For example: "2023-06-09T10:30:00+03:00" represents June 9th, '
" 2023, at 10:30 AM in a time zone with a positive offset of 3 "
" hours from Coordinated Universal Time (UTC).",
)
class SlackScheduleMessage(SlackBaseTool):
"""Tool for scheduling a message in Slack."""
name: str = "schedule_message"
description: str = (
"Use this tool to schedule a message to be sent on a specific date and time."
)
args_schema: Type[ScheduleMessageSchema] = ScheduleMessageSchema
def _run(
self,
message: str,
channel: str,
timestamp: str,
run_manager: Optional[CallbackManagerForToolRun] = None,
) -> str:
try:
unix_timestamp = dt.timestamp(dt.strptime(timestamp, UTC_FORMAT))
result = self.client.chat_scheduleMessage(
channel=channel, text=message, post_at=unix_timestamp
)
output = "Message scheduled: " + str(result)
return output
except Exception as e:
return "Error scheduling message: {}".format(e)

View File

@@ -0,0 +1,42 @@
from typing import Optional, Type
from langchain_core.callbacks import CallbackManagerForToolRun
from pydantic import BaseModel, Field
from langchain_community.tools.slack.base import SlackBaseTool
class SendMessageSchema(BaseModel):
"""Input for SendMessageTool."""
message: str = Field(
...,
description="The message to be sent.",
)
channel: str = Field(
...,
description="The channel, private group, or IM channel to send message to.",
)
class SlackSendMessage(SlackBaseTool):
"""Tool for sending a message in Slack."""
name: str = "send_message"
description: str = (
"Use this tool to send a message with the provided message fields."
)
args_schema: Type[SendMessageSchema] = SendMessageSchema
def _run(
self,
message: str,
channel: str,
run_manager: Optional[CallbackManagerForToolRun] = None,
) -> str:
try:
result = self.client.chat_postMessage(channel=channel, text=message)
output = "Message sent: " + str(result)
return output
except Exception as e:
return "Error creating conversation: {}".format(e)

View File

@@ -0,0 +1,43 @@
"""Slack tool utils."""
from __future__ import annotations
import logging
import os
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from slack_sdk import WebClient
logger = logging.getLogger(__name__)
def login() -> WebClient:
"""Authenticate using the Slack API."""
try:
from slack_sdk import WebClient
except ImportError as e:
raise ImportError(
"Cannot import slack_sdk. Please install the package with \
`pip install slack_sdk`."
) from e
if "SLACK_BOT_TOKEN" in os.environ:
token = os.environ["SLACK_BOT_TOKEN"]
client = WebClient(token=token)
logger.info("slack login success")
return client
elif "SLACK_USER_TOKEN" in os.environ:
token = os.environ["SLACK_USER_TOKEN"]
client = WebClient(token=token)
logger.info("slack login success")
return client
else:
logger.error(
"Error: The SLACK_BOT_TOKEN or SLACK_USER_TOKEN \
environment variable have not been set."
)
UTC_FORMAT = "%Y-%m-%dT%H:%M:%S%z"
"""UTC format for datetime objects."""