Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new events subscription API #23

Merged
merged 6 commits into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion kenar/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from .addon import *
from .app import ClientConfig, Client
from .asset import *
from .chatmessage import *
from .events import *
from .finder import *
from .icons import *
from .image import *
from .oauth import *
from .widgets import *
from .asset import *
70 changes: 53 additions & 17 deletions kenar/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@
GetColorsResponse,
)
from kenar.chatmessage import (
SetNotifyChatPostConversationsRequest,
SendMessageV2Request,
SendMessageV2Response,
SetNotifyChatPostConversationsResponse,
ChatBotSendMessageRequest,
ChatBotSendMessageResponse,
)
from kenar.events import (
RegisterEventSubscriptionRequest,
RegisterEventSubscriptionResponse,
)
from kenar.finder import (
SearchPostRequest,
Expand Down Expand Up @@ -64,41 +68,45 @@ class ChatService:
def __init__(self, client: httpx.Client):
self._client = client

def set_notify_chat_post_conversations(
def send_message(
self,
access_token: str,
data: SetNotifyChatPostConversationsRequest,
data: SendMessageV2Request,
max_retry=3,
retry_delay=1,
) -> SetNotifyChatPostConversationsResponse:
) -> SendMessageV2Response:
@retry(max_retries=max_retry, delay=retry_delay)
def send_request():
return self._client.post(
url="/v1/open-platform/notify/chat/post-conversations",
url="/v2/open-platform/chat/conversation",
content=data.json(),
headers={ACCESS_TOKEN_HEADER_NAME: access_token},
)

send_request()
return SetNotifyChatPostConversationsResponse()
rsp = send_request()
return SendMessageV2Response(**rsp.json())

def send_message(
def send_chatbot_message(
self,
access_token: str,
data: SendMessageV2Request,
data: ChatBotSendMessageRequest,
max_retry=3,
retry_delay=1,
) -> SendMessageV2Response:
@retry(max_retries=max_retry, delay=retry_delay)
def send_request():
return self._client.post(
url="/v2/open-platform/chat/conversation",
content=data.json(),
headers={ACCESS_TOKEN_HEADER_NAME: access_token},
)
if data.user_id:
return self._client.post(
url=f"/experimental/open-platform/chat/bot/users/{data.user_id}/messages",
content=data.json(),
)
else:
return self._client.post(
url=f"/experimental/open-platform/chat/bot/conversations/{data.conversation_id}/messages",
content=data.json(),
)

rsp = send_request()
return SendMessageV2Response(**rsp.json())
return ChatBotSendMessageResponse(**rsp.json())


class FinderService:
Expand Down Expand Up @@ -174,6 +182,29 @@ def send_request():
return GetUserPostsResponse(**rsp.json())


class EventsService:
def __init__(self, client: httpx.Client):
self._client = client

def register_event_subscription(
self,
access_token: str,
data: RegisterEventSubscriptionRequest,
max_retry=3,
retry_delay=1,
) -> RegisterEventSubscriptionResponse:
@retry(max_retries=max_retry, delay=retry_delay)
def send_request():
return self._client.post(
url="/v1/open-platform/events/subscriptions",
content=data.json(),
headers={ACCESS_TOKEN_HEADER_NAME: access_token},
)

send_request()
return RegisterEventSubscriptionResponse()


class AddonService:
def __init__(self, client: httpx.Client):
self._client = client
Expand Down Expand Up @@ -526,13 +557,18 @@ def __init__(self, conf: ClientConfig):
)
self._finder = FinderService(self._client)
self._chat = ChatService(self._client)
self._events = EventsService(self._client)
self._addon = AddonService(self._client)
self._asset = AssetService(self._client)

@property
def chat(self):
return self._chat

@property
def events(self):
return self._events

@chat.setter
def chat(self, service: ChatService):
if not isinstance(service, ChatService):
Expand Down
43 changes: 31 additions & 12 deletions kenar/chatmessage.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from enum import Enum
from typing import Optional
from typing import Optional, List

from pydantic import BaseModel
from pydantic import BaseModel, Field

from kenar.icons import IconName
from kenar.widgets.action import Action


class BotButton(BaseModel):
Expand All @@ -19,16 +22,6 @@ class ButtonData(BaseModel):
action: Action


class SetNotifyChatPostConversationsRequest(BaseModel):
post_token: str
endpoint: str
identification_key: str


class SetNotifyChatPostConversationsResponse(BaseModel):
pass


class SendMessageV2Request(BaseModel):
user_id: str
peer_id: str
Expand All @@ -53,3 +46,29 @@ class PostConversationsNotificationRegisterPayload(BaseModel):
class PostConversationsNotificationPayload(BaseModel):
registration_payload: PostConversationsNotificationRegisterPayload
identification_key: str


class ChatButton(BaseModel):
action: Action
caption: str
icon: IconName


class ChatButtonRow(BaseModel):
buttons: List[ChatButton] = Field(default_factory=list)


class ChatButtonGrid(BaseModel):
rows: List[ChatButtonRow] = Field(default_factory=list)


class ChatBotSendMessageRequest(BaseModel):
conversation_id: Optional[str] = None
user_id: Optional[str] = None
text_message: str
media_token: Optional[str] = None
buttons: Optional[ChatButtonGrid] = None


class ChatBotSendMessageResponse(BaseModel):
pass
20 changes: 20 additions & 0 deletions kenar/events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from dataclasses import field
from enum import Enum
from typing import Any, Dict
from typing import Optional

from pydantic import BaseModel


class RegisterEventSubscriptionRequest(BaseModel):
class EventType(Enum):
UNKNOWN = 'UNKNOWN'
NEW_MESSAGE_ON_POST = 'NEW_MESSAGE_ON_POST'

event_type: EventType
event_resource_id: Optional[str] = field(default="")
metadata: Optional[Dict[str, Any]] = field(default=None)


class RegisterEventSubscriptionResponse(BaseModel):
pass
13 changes: 12 additions & 1 deletion kenar/widgets/action.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Dict
from typing import Dict, Optional

from pydantic import BaseModel


def get_action(link: str) -> Dict:
Expand All @@ -15,3 +17,12 @@ def get_action(link: str) -> Dict:

def get_link_from_action(action: Dict) -> str:
return action.get("open_direct_link", "")


class OpenServerLink(BaseModel):
data: dict


class Action(BaseModel):
open_direct_link: Optional[str] = None
open_server_link: Optional[OpenServerLink] = None
11 changes: 5 additions & 6 deletions samples/sample_chat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from kenar import (
SendMessageV2Request,
BotButton,
SetNotifyChatPostConversationsRequest,
RegisterEventSubscriptionRequest,
)
from samples.sample_app import app

Expand Down Expand Up @@ -33,11 +33,10 @@
access_token="ACCESS_TOKEN_HERE",
)

app.chat.set_notify_chat_post_conversations(
data=SetNotifyChatPostConversationsRequest(
post_token="gZ6QmeWD",
endpoint="https://test2.com",
identification_key="thest-identification-key",
app.events.register_event_subscription(
data=RegisterEventSubscriptionRequest(
event_type=RegisterEventSubscriptionRequest.EventType.NEW_MESSAGE_ON_POST,
event_resource_id="gZ6QmeWD",
),
access_token="ACCESS_TOKEN_HERE",
)