ECSS Chat Client
Python клиент для работы с ECSS Chat.
Install
pip install ecss-chat-client
GetStarted
from ecss_chat_client import Client
Basic HTTP Client
Basic user
client=Client(
server=Params.SERVER_IP,
proto=Params.PROTOCOL,
port=Params.SERVER_PORT,
verify=False,
)
client.create_session(
username=USERNAME, password=PASSWORD,
)
Basic bot
client=Client(
server=Params.SERVER_IP,
proto=Params.PROTOCOL,
port=Params.SERVER_PORT,
verify=False,
)
client.create_bot_session(token='You bot token')
Simple Echo Bot
class SimpleBot(Client):
@decorator_service.ws_ping_wrapper(logger=logger)
def on_message(self, ws: WebSocketApp, message):
logger.info('### message ###')
try:
data = json.loads(message)
received_message = data.get('msg')
if received_message == 'connected':
self.basic_websocket.auth()
if received_message == 'result' and data.get('result').get('id'):
self.basic_websocket.subscribe_notification()
if received_message == 'changed':
payload = data['fields']['args'][0]['payload']
payload_type = payload['type']
if payload_type == RoomTypes.DIRECT:
if len(payload['message']['msg']) > 0:
message = payload['message']['msg']
room_id = payload['rid']
self.messages.send(
text=message,
room_id=room_id,
)
except json.JSONDecodeError:
pass
@staticmethod
def on_error(ws: WebSocketApp, error):
logger.error(error)
@staticmethod
def on_close(ws: WebSocketApp, close_status_code, close_msg):
logger.warning('### closed ###')
@staticmethod
def send_message(client: Client, text, rid):
client.messages.send(text, room_id=rid)
if __name__ == '__main__':
bot_client = SimpleBot(server=SERVER, port=PORT, verify=False)
bot_client.create_bot_session(token=TOKEN)
logger.add('bot.log', rotation='500 MB')
bot_client.basic_websocket.init_ws_client(
on_message_handler=bot_client.on_message,
on_error_handler=bot_client.on_error,
on_close_handler=bot_client.on_close,
token=TOKEN,
client_uid=UID,
logger=logger,
websocket_trace=True,
)
Вы можете обработать событие "on_pong" с помощью декоратора
@decorator_service.ws_ping_wrapper(logger=logger)
def on_message(self, ws: WebSocketApp, message):
logger.info('### message ###')
try:
data = json.loads(message)
received_message = data.get('msg')
Вы можете обработать событие "on_pong" с помощью функции "ping_handler"
def on_message(self, ws: WebSocketApp, message):
logger.info('### message ###')
try:
data = json.loads(message)
received_message = data.get('msg')
if received_message == 'ping':
self.basic_websocket.ping_handler()
Можно не передавать логер в декоратор с учетом, что кастомный логгер не был добавлен в "init_ws_client"
@decorator_service.ws_ping_wrapper()
def on_message(self, ws: WebSocketApp, message):
logger.info('### message ###')
try:
data = json.loads(message)
received_message = data.get('msg')
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ecss_chat_client-1.5.50.tar.gz
(25.8 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ecss_chat_client-1.5.50.tar.gz.
File metadata
- Download URL: ecss_chat_client-1.5.50.tar.gz
- Upload date:
- Size: 25.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d3a9aef9ce62eb947c3c4a8da227c5f16d2e17b81b9c79ac14f15fbed6d8204
|
|
| MD5 |
5917a4e99b3db51c29edb4fdd6c56a2f
|
|
| BLAKE2b-256 |
66ab14216961be1c712f8ff96afa3ad82f14cbff9f117d4d50c8692f30ccfb00
|
File details
Details for the file ecss_chat_client-1.5.50-py3-none-any.whl.
File metadata
- Download URL: ecss_chat_client-1.5.50-py3-none-any.whl
- Upload date:
- Size: 37.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d0dc8265c6f2125a4b3498db4624b07f7496ff226efc4aa5aca0f0f3281bb52
|
|
| MD5 |
d947ab1d19766de7ae6c9d6c5145560d
|
|
| BLAKE2b-256 |
9ab1a0dbea6d9731a1e643c57602a14dea36bc6b6b49e79160801d431abb2047
|