Skip to main content

GCHAT Close Conversations

This project automates sending alerts and closing inactive conversations on the GCHAT platform, using API integration.

Repository

https://github.com/Gnew-Solucoes-IP-Ltda/gchat-sdk

Features

  • Automatically sends alert messages to inactive contacts.
  • Automatically closes conversations based on configurable criteria.
  • Flexible configuration via environment variables.

Project Structure

.
├── gchat_sdk/
│   ├── controllers/
│   ├── entities/
│   ├── factories/
│   ├── providers/
│   ├── tests/
│   ├── main.py
│   ├── settings.py
│   └── utils.py
├── examples/
│   └── finish_chats.py
├── .env
├── requirements.txt
└── LICENSE

Installation

  1. Create and activate a virtual environment:

    python3 -m venv venv
    source venv/bin/activate
    
  2. Install dependencies:

    pip install gchat-sdk
    
  3. Configure environment variables in the .env file:

    URL_API=https://api.gchat.app.br
    TOKEN_API=your_token
    END_CHATS_WITH_ATTENDANTS_LAST_MESSAGE=True
    END_CHATS_WITH_CONTACTS_LAST_MESSAGE=True
    TIMEOUT=1
    ALERT_TIME=0.1
    

Usage

Run the example to send alerts and close inactive conversations:

import os
from dotenv import load_dotenv
from gchat_sdk.controllers.chat_controller import ChatController
from gchat_sdk.providers.chatbot_provider import ChatBotProvider


load_dotenv()


URL_API = os.environ.get('URL_API')
TOKEN_API = os.environ.get('TOKEN_API')

END_CHATS_WITH_ATTENDANTS_LAST_MESSAGE = True
END_CHATS_WITH_CONTACTS_LAST_MESSAGE = True
TIMEOUT = 2
ALERT_TIME = 0.1
ALERT_MESSAGE_TEXT = '''
🔔 Olá! Percebemos que você está inativo há algum tempo.
Para otimizar nosso atendimento, esta conversa será encerrada.
Caso precise de algo, é só nos chamar novamente.

👋 Agradecemos o contato e estamos à disposição!
'''

provider = ChatBotProvider(
    base_url=URL_API,
    access_token=TOKEN_API
)
controller = ChatController(provider, ALERT_MESSAGE_TEXT)
data = controller.alert_chats(alert_time_in_hour=ALERT_TIME)
print('send message alert', data)
data = controller.finish_chats(
    END_CHATS_WITH_ATTENDANTS_LAST_MESSAGE, 
    END_CHATS_WITH_CONTACTS_LAST_MESSAGE, 
    TIMEOUT
)
print('closed chats', data)

Native channel (guest chat)

NativeChannelController talks to the Gchat native channel (the public web chat at https://access.gchat.app.br/room/<handle>) as a guest visitor. It covers the whole lifecycle: bootstrap, guest session, conversation start, sending messages, polling and finishing.

from gchat_sdk.controllers import NativeChannelController
from gchat_sdk.providers import NativeChannelProvider

provider = NativeChannelProvider(base_url='https://access.gchat.app.br')
controller = NativeChannelController(provider, handle='your_channel_handle')

controller.bootstrap()
controller.create_guest_session('Name', 'name@example.com', '+5511999999999')  # phone in E.164
controller.start_conversation()
controller.send_message('Hello')

for messages in controller.poll(interval=10):  # stops when the conversation is closed/expired
    for message in messages:
        print(message['direction'], message.get('text'))

controller.finalize_conversation()
controller.logout()
  • device_id identifies the device; persist controller.device_id and pass it back to the constructor to reuse it across runs.
  • Non-2xx responses raise NativeChannelError with status, code (e.g. RATE_LIMIT_EXCEEDED, CONVERSATION_START_REQUIRED, SESSION_INVALID) and body.
  • poll() acknowledges incoming messages as delivered and read; pass ack=False and call controller.ack(ids, 'read') yourself to control read receipts.

See examples/native_channel_chat.py.

Testing

Unit tests are located in gchat_sdk/tests/. To run the tests using unittest:

python -m unittest discover gchat_sdk/tests/

License

This project is licensed under the MIT License. See the LICENSE file for details.

Metadata

Release files for gchat-sdk 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gchat-sdk 0.2.0
File Size Uploaded Provenance Actions
gchat_sdk-0.2.0.tar.gz 12.4 kB Without provenance Details for gchat_sdk-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for gchat-sdk 0.2.0
File Interpreter ABI Platform Actions
gchat_sdk-0.2.0-py3-none-any.whl Python 3 none any Details for gchat_sdk-0.2.0-py3-none-any.whl

Total release size: 25.6 kB

Release files / gchat_sdk-0.2.0.tar.gz

Download URL gchat_sdk-0.2.0.tar.gz
Size 12.4 kB
Tags Source
SHA-256 checksum
How to use checksums
72ea38cb9e3e6d8bf3890acb1f07b9de67bee610ce2f14833ea88e7d99822360
BLAKE2b-256 checksum
How to use checksums
70419020a8d2455d5c00fa29ca6e24cc91be61acbb23e054841786504b80d41e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / gchat_sdk-0.2.0-py3-none-any.whl

Download URL gchat_sdk-0.2.0-py3-none-any.whl
Size 13.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ea5008a1103d3c1008f935298807d65fa62c0519ce4041caa50ed458054bf2d2
BLAKE2b-256 checksum
How to use checksums
5cd4f00b2ac1c7aa2c25765eae053a64544bea836114f282c9def3233cfca827
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for gchat-sdk 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page