1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
"""
Message - Read and send messages
TODO: Support group messaging
"""
from xbox.webapi.api.provider.baseprovider import BaseProvider
from xbox.webapi.api.provider.message.models import (
ConversationResponse,
InboxResponse,
SendMessageResponse,
)
class MessageProvider(BaseProvider):
MSG_URL = "https://xblmessaging.xboxlive.com"
HEADERS_MESSAGE = {"x-xbl-contract-version": "1"}
HEADERS_HORIZON = {"x-xbl-contract-version": "2"}
async def get_inbox(self, max_items: int = 100, **kwargs) -> InboxResponse:
"""
Get messages
Returns:
:class:`InboxResponse`: Inbox Response
"""
url = f"{self.MSG_URL}/network/Xbox/users/me/inbox"
params = {"maxItems": max_items}
resp = await self.client.session.get(
url, params=params, headers=self.HEADERS_MESSAGE, **kwargs
)
resp.raise_for_status()
return InboxResponse(**resp.json())
async def get_conversation(
self, xuid: str, max_items: int = 100, **kwargs
) -> ConversationResponse:
"""
Get detailed conversation info
Args:
xuid: Xuid of user having a conversation with
Returns:
:class:`ConversationResponse`: Conversation Response
"""
url = f"{self.MSG_URL}/network/Xbox/users/me/conversations/users/xuid({xuid})"
params = {"maxItems": max_items}
resp = await self.client.session.get(
url, params=params, headers=self.HEADERS_MESSAGE, **kwargs
)
resp.raise_for_status()
return ConversationResponse(**resp.json())
async def delete_conversation(
self, conversation_id: str, horizon: str, **kwargs
) -> bool:
"""
Delete message
**NOTE**: Returns HTTP Status Code **200** on success
Args:
conversation_id: Message Id
horizon: Delete horizon from get conversation response
Returns: True on success, False otherwise
"""
url = f"{self.MSG_URL}/network/Xbox/users/me/conversations/horizon"
post_data = {
"conversations": [
{
"conversationId": conversation_id,
"conversationType": "OneToOne",
"horizonType": "Delete",
"horizon": horizon,
}
]
}
resp = await self.client.session.put(
url, json=post_data, headers=self.HEADERS_HORIZON, **kwargs
)
return resp.status_code == 200
async def delete_message(
self, conversation_id: str, message_id: str, **kwargs
) -> bool:
"""
Delete message
**NOTE**: Returns HTTP Status Code **200** on success
Args:
conversation_id: Conversation Id
message_id: Message Id
Returns: True on success, False otherwise
"""
url = f"{self.MSG_URL}/network/Xbox/users/me/conversations/{conversation_id}/messages/{message_id}"
resp = await self.client.session.delete(
url, headers=self.HEADERS_MESSAGE, **kwargs
)
return resp.status_code == 200
async def send_message(
self, xuid: str, message_text: str, **kwargs
) -> SendMessageResponse:
"""
Send message to an xuid
Args:
xuid: Xuid
message_text: Message text
Returns:
:class:`SendMessageResponse`: Send Message Response
"""
if len(message_text) > 256:
raise ValueError("Message text exceeds max length of 256 chars")
url = f"{self.MSG_URL}/network/Xbox/users/me/conversations/users/xuid({xuid})"
post_data = {
"parts": [
{
"contentType": "text",
"version": 0,
"text": message_text,
}
]
}
resp = await self.client.session.post(
url, json=post_data, headers=self.HEADERS_MESSAGE, **kwargs
)
resp.raise_for_status()
return SendMessageResponse(**resp.json())
|