Giter Club home page Giter Club logo

hubspot-api's Introduction

HubSpot Private API

Python library to interact with HubSpot's Private API

This is a Python library of HubSpot's Private API, which was reverse-engineered from their Mobile App. It allows users to do any kind of LLM automation / auto-reply (to live chats).

It was developed because HubSpot doesn't bother making their Public API feature-complete, and have had some modules in BETA for over a year (khm.. Conversations).

Additional features, which their Public API doesn't support:

  • Conversation: access Live Chat channels
  • Conversation: Assign the conversation to an agent
  • Conversation: Tag an agent inside a comment

Demo script

from hubspot_api import HubSpotApi

def cb_auth() -> str: # In case of 2FA, you'll need to return the code
    code = input('Enter 2FA code: ')
    return code

hs = HubSpotApi('[email protected]', 'superSecretPw', cb_auth)

# You can also do hs.conversations.get_threads() to get all threads
threads = hs.conversations.get_unassigned_threads()
# Needed for assigning threads to agents
agents = hs.conversations.get_agents()

for t in threads:
    print('---------\nSubject:',t.subject, t.timestamp)
    msgs = t.read_messages()
    email_msgs = [f"FROM: {msg.fromName}, AT: {msg.timestamp}, EMAIL TEXT:\n{msg.text}" for msg in msgs]
    full_conversation = f'EMAIL SUBJECT: {t.subject}\n' + '\n\n'.join(email_msgs).lower()

    # Do some smart LLM stuff here

    if "api" in full_conversation or "code" in full_conversation:
        t.assign(agents.find('John Doe')) # Tech support
    elif "shipping" in full_conversation:
        t.assign(agents.find('Erik The Red')) # Ops/shipping
    elif "digital agency" in full_conversation:
        # They are trying to sell us something
        t.send_message("Thank you for your email, but we are not interested.")
        t.close()
    elif "tiktok" in full_conversation:
        t.spam() # TikTok? Spam for sure
    else:
        # Writing a comment (and tagging an agent) is also supported
        agent = agents.find('John Doe')
        t.comment(f'Hi {agent}, could you assign this to the best person?')

Module support

While HubSpot has many different modules (contacts, deals, tickets, etc.), most of them work as expected with their public API. Only Conversations are in "BETA", and have been for over a year now.

Installation

To install this Python package, run the following command:

pip install hubspot-api

Liability

Note: This unofficial API library is not endorsed by HubSpot and likely violates their Terms of Service. Use it at your own risk; the creator assumes no liability for any consequences.

hubspot-api's People

Contributors

erol444 avatar

Stargazers

Kfwebdev avatar  avatar

Watchers

 avatar

hubspot-api's Issues

[Feature Request] Conversation Comments

Comments are a bit more tricky, as they use ably.io for their real-time aspect. There are 3 steps (REST calls) required to post a comment in a conversation, and I am not quite sure how to get everything; specifically the initial KeyName (13CharKey). After that, everything is fairly straightforward.

Get request token

POST https://hubspot-eu-rest.ably.io/keys/13CharKey/requestToken?rnd=17digitNum
So for rnd I assume it's just a random number, but I am not sure how/when app gets the 13CharKey. Response is something like this:

{
    "capability": "{\"prod.messages.CUSTOM-list-123123-123123-123123\":[\"history\",\"presence\",\"publish\",\"subscribe\"],\"prod.messages.CUSTOM-list-222-222-222\":[\"history\",\"presence\",\"publish\"",
    "clientId": "AGENT-123123123",
    "expires": 1707689581429,
    "issued": 1707684886129,
    "keyName": "13CharKey",
    "token": "57charToken"
}

Open connection

Now that we have 57charToken, we can open a connection.

GET https://hubspot-eu-rest.ably.io/comet/connect?access_token=57charToken&stream=false&heartbeats=true&v=2&agent=ably-js%252F1.2.43%2520browser&rnd=17238796190672098

Which returns something like this:

[
    {
        "action": 4,
        "connectionDetails": {
            "clientId": "AGENT-123123123",
            "connectionKey": "myConnectionKey",
            "connectionStateTtl": 120000,
            "maxFrameSize": 262144,
            "maxIdleInterval": 15000,
            "maxInboundRate": 200,
            "maxMessageSize": 184320,
            "maxOutboundRate": 200,
            "serverId": "frontend.7d71.4.eu-central-1-SomeId"
        },
        "connectionId": "connectionId"
    }
]

I believe only myConnectionKey is required.

Post comment

POST https://hubspot-eu-rest.ably.io/comet/myConnectionKey/send?access_token=57charToken&rnd=9029143974461677

With a JSON body of:

[
    {
        "action": 15,
        "channel": "thread:v2:prod:private:123123:123123:2fa7e187-1231-1231-asdf-asdasdasd",
        "messages": [
            {
                "data": "{\"@type\":\"THREAD_COMMENT\",\"sender\":{\"@type\":\"AGENT_SENDER\",\"id\":222333},\"senders\":[{\"actorId\":\"A-60366737\",\"deliveryIdentifier\":null,\"senderField\":null,\"name\":null}],\"timestamp\":1707993838371,\"text\":\"@Areb Areb Hi123\",\"richText\":\"<strong data-at-mention data-user-id=\\\"60366737\\\" data-search-text=\\\"Areb\\\" data-search-value=\\\"[email protected]\\\">@Areb</strong> Hi123\",\"hasMore\":false,\"id\":\"123123123\",\"status\":{\"messageStatus\":\"SENT\",\"timestamp\":null,\"sendFailure\":null},\"attachments\":[{\"@type\":\"MENTIONS\",\"userIds\":[123123]}],\"messageDeletedStatus\":\"NOT_DELETED\",\"clientType\":\"MOBILE\",\"genericChannelId\":null}",
                "id": "123123123",
                "name": "1"
            }
        ],
        "msgSerial": 1
    }
]

Channel is the channel of the conversation, and id can be random hex str.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.