Skip to main content

This is the simplest module HTTP API EcssChatServer

Project description

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')

Project details


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.49.tar.gz (25.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ecss_chat_client-1.5.49-py3-none-any.whl (37.7 kB view details)

Uploaded Python 3

File details

Details for the file ecss_chat_client-1.5.49.tar.gz.

File metadata

  • Download URL: ecss_chat_client-1.5.49.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

Hashes for ecss_chat_client-1.5.49.tar.gz
Algorithm Hash digest
SHA256 5e730da7057f28bfc172153a8257c99da7274be99e077130d01014d38af2ac46
MD5 ba760741cb21dc0d3bd4bbbbac9fed5f
BLAKE2b-256 36cf87eceeed5a085fe7078f9f7ce1fae3b274f9ce6a6f2270a26090fd45a549

See more details on using hashes here.

File details

Details for the file ecss_chat_client-1.5.49-py3-none-any.whl.

File metadata

File hashes

Hashes for ecss_chat_client-1.5.49-py3-none-any.whl
Algorithm Hash digest
SHA256 c92ed482f1c330a2b6d76b9a62ac26772e6574dbc78a3a2712497c9acfc7f36a
MD5 d50ab51314ef2b2ecad9f8d81b0ee2dd
BLAKE2b-256 d4092c192e4a31e74f3ced59f81a36c9b1a9066605ad017ed7dbaa39a16b887b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page