mattermost/python-sdk/build/lib/mattermost_plugin/__init__.py
Nick Misasi a26942bde1 fix(python-sdk): accept dict responses from ExecuteCommand hook
- Add _to_command_response_proto helper to convert dict/wrapper/protobuf
  to CommandResponse protobuf
- Plugin developers can now return plain dicts with response_type, text, etc.
- Also supports CommandResponse wrapper class and raw protobufs
- Export CommandResponse from mattermost_plugin package

This fixes the "ExecuteCommand returned unexpected type: <class 'dict'>"
error when Python plugin handlers return dict responses.
2026-01-20 10:08:47 -05:00

99 lines
2.7 KiB
Python

# Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
# See LICENSE.txt for license information.
"""
Mattermost Plugin SDK for Python.
This SDK provides a Pythonic interface to the Mattermost Plugin API via gRPC.
Python plugins can use this SDK to interact with the Mattermost server.
Plugin development::
from mattermost_plugin import Plugin, hook, HookName
class MyPlugin(Plugin):
@hook(HookName.OnActivate)
def on_activate(self) -> None:
self.logger.info("Plugin activated!")
version = self.api.get_server_version()
self.logger.info(f"Server version: {version}")
@hook(HookName.MessageWillBePosted)
def filter_messages(self, context, post):
if "spam" in post.message.lower():
return None, "Spam detected"
return post, ""
if __name__ == "__main__":
from mattermost_plugin.server import run_plugin
run_plugin(MyPlugin)
Basic API client usage::
from mattermost_plugin import PluginAPIClient, PluginAPIError
# Use with context manager for automatic cleanup
with PluginAPIClient(target="localhost:50051") as client:
try:
version = client.get_server_version()
print(f"Server version: {version}")
except PluginAPIError as e:
print(f"Error: {e}")
Async usage::
from mattermost_plugin import AsyncPluginAPIClient
async with AsyncPluginAPIClient(target="localhost:50051") as client:
version = await client.get_server_version()
print(f"Server version: {version}")
"""
from mattermost_plugin.client import PluginAPIClient
from mattermost_plugin.async_client import AsyncPluginAPIClient
from mattermost_plugin.exceptions import (
PluginAPIError,
NotFoundError,
PermissionDeniedError,
ValidationError,
AlreadyExistsError,
UnavailableError,
convert_grpc_error,
)
from mattermost_plugin.hooks import (
HookName,
hook,
HookRegistrationError,
)
from mattermost_plugin.plugin import Plugin
from mattermost_plugin.runtime_config import RuntimeConfig, load_runtime_config
from mattermost_plugin._internal.wrappers import Command, CommandResponse
__version__ = "0.1.0"
__all__ = [
# Plugin development
"Plugin",
"hook",
"HookName",
"HookRegistrationError",
"Command",
"CommandResponse",
# Configuration
"RuntimeConfig",
"load_runtime_config",
# Clients
"PluginAPIClient",
"AsyncPluginAPIClient",
# Exceptions
"PluginAPIError",
"NotFoundError",
"PermissionDeniedError",
"ValidationError",
"AlreadyExistsError",
"UnavailableError",
# Utilities
"convert_grpc_error",
# Version
"__version__",
]