Skip to main content

Google's Business Messages: Python Client

Business Messages is a mobile conversational channel that combines entry points on Google Maps, Search, and brand websites to create rich, asynchronous messaging experiences.

This document contains an API reference, samples, and other resources useful to developing Python applications. For additional help developing Business Messages applications, in Python and other languages, see our Business Messages quickstart guide.

Documentation

The documentation for the Business Messages API can be found here.

Quickstart

Before you begin

  1. Register with Business Messages.
  2. Once registered, follow the instructions to enable the APIs for your project.

Installation

Mac/Linux

python -m venv <your-env>
source <your-env>/bin/activate
<your-env>/bin/pip install google-businessmessages

Windows

python -m venv <your-env>
<your-env>\Scripts\activate
<your-env>\Scripts\pip.exe install google-businessmessages

Supported Python Versions

Python 3.5, 3.6 and 3.7, and 3.8 are fully supported and tested.

Using the client library

from businessmessages import businessmessages_v1_client as bm_client
from businessmessages.businessmessages_v1_messages import (
    BusinessmessagesConversationsMessagesCreateRequest,
    BusinessMessagesMessage,
    BusinessMessagesRepresentative)

credentials = ServiceAccountCredentials.from_json_keyfile_name(
    'your-service-account-key-file-location',
    scopes=['https://www.googleapis.com/auth/businessmessages'])

client = bm_client.BusinessmessagesV1(credentials=credentials)

conversation_id = 'valid-conversation-id'

message = BusinessMessagesMessage(
    messageId=str(uuid.uuid4().int),
    representative=BusinessMessagesRepresentative(
        representativeType=BusinessMessagesRepresentative.RepresentativeTypeValueValuesEnum.BOT
    ),
    text='Hello, World!')

# Create the message request
create_request = BusinessmessagesConversationsMessagesCreateRequest(
    businessMessagesMessage=message,
    parent='conversations/' + conversation_id)

# Send the message
bm_client.BusinessmessagesV1.ConversationsMessagesService(
    client=client).Create(request=create_request)

Sample usage

Samples below assume a similar library initialization as shown in the Using the client library section.

Sending a text message

message = BusinessMessagesMessage(
    messageId=str(uuid.uuid4().int),
    representative=BusinessMessagesRepresentative(
        representativeType=BusinessMessagesRepresentative.RepresentativeTypeValueValuesEnum.BOT
    ),
    text='Hello, World!')

# Create the message request
create_request = BusinessmessagesConversationsMessagesCreateRequest(
    businessMessagesMessage=message,
    parent='conversations/' + conversation_id)

# Send the message
bm_client.BusinessmessagesV1.ConversationsMessagesService(
    client=client).Create(request=create_request)

Sending a text message with suggested replies and actions

message = BusinessMessagesMessage(
    messageId=str(uuid.uuid4().int),
    representative=BusinessMessagesRepresentative(
        representativeType=BusinessMessagesRepresentative.RepresentativeTypeValueValuesEnum.BOT
    ),
    text='Hello, World!',
    suggestions=[
        BusinessMessagesSuggestion(
            reply=BusinessMessagesSuggestedReply(
                text='Sample Chip',
                postbackData='sample_chip')
            ),
        BusinessMessagesSuggestion(
            action=BusinessMessagesSuggestedAction(
                text='URL Action',
                postbackData='url_action',
                openUrlAction=BusinessMessagesOpenUrlAction(
                    url='https://www.google.com'))
            ),
        BusinessMessagesSuggestion(
            action=BusinessMessagesSuggestedAction(
                text='Dial Action',
                postbackData='dial_action',
                dialAction=BusinessMessagesDialAction(
                    phoneNumber='+12223334444'))
            ),
        ])

# Create the message request
create_request = BusinessmessagesConversationsMessagesCreateRequest(
    businessMessagesMessage=message,
    parent='conversations/' + conversation_id)

# Send the message
bm_client.BusinessmessagesV1.ConversationsMessagesService(
    client=client).Create(request=create_request)

Sending a rich card

message = BusinessMessagesMessage(
    messageId=str(uuid.uuid4().int),
    representative=BusinessMessagesRepresentative(
        representativeType=BusinessMessagesRepresentative.RepresentativeTypeValueValuesEnum.BOT
    ),
    richCard=BusinessMessagesRichCard(
        standaloneCard=BusinessMessagesStandaloneCard(
            cardContent=BusinessMessagesCardContent(
                title='Business Messages!!!',
                description='This is an example rich card',
                suggestions=[
                  BusinessMessagesSuggestion(
                      reply=BusinessMessagesSuggestedReply(
                          text='Sample Chip',
                          postbackData='sample_chip')
                      )
                  ],
                media=BusinessMessagesMedia(
                    height=BusinessMessagesMedia.HeightValueValuesEnum.MEDIUM,
                    contentInfo=BusinessMessagesContentInfo(
                        fileUrl='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
                        forceRefresh=False
                    ))
                ))))

# Create the message request
create_request = BusinessmessagesConversationsMessagesCreateRequest(
    businessMessagesMessage=message,
    parent='conversations/' + conversation_id)

# Send the message
bm_client.BusinessmessagesV1.ConversationsMessagesService(
    client=client).Create(request=create_request)
message = BusinessMessagesMessage(
    messageId=str(uuid.uuid4().int),
    representative=BusinessMessagesRepresentative(
        representativeType=BusinessMessagesRepresentative.RepresentativeTypeValueValuesEnum.BOT
    ),
    richCard=BusinessMessagesRichCard(
      carouselCard=BusinessMessagesCarouselCard(
        cardWidth=BusinessMessagesCarouselCard.CardWidthValueValuesEnum.MEDIUM,
        cardContents=[
          BusinessMessagesCardContent(
              title='Card #1',
              description='The description for card #1',
              suggestions=[
                BusinessMessagesSuggestion(
                        reply=BusinessMessagesSuggestedReply(
                            text='Card #1',
                            postbackData='card_1')
                        )
              ],
              media=BusinessMessagesMedia(
                  height=BusinessMessagesMedia.HeightValueValuesEnum.MEDIUM,
                  contentInfo=BusinessMessagesContentInfo(
                      fileUrl='https://storage.googleapis.com/kitchen-sink-sample-images/cute-dog.jpg',
                      forceRefresh=False))),
          BusinessMessagesCardContent(
              title='Card #2',
              description='The description for card #2',
              suggestions=[
                BusinessMessagesSuggestion(
                        reply=BusinessMessagesSuggestedReply(
                            text='Card #2',
                            postbackData='card_2')
                        )
              ],
              media=BusinessMessagesMedia(
                  height=BusinessMessagesMedia.HeightValueValuesEnum.MEDIUM,
                  contentInfo=BusinessMessagesContentInfo(
                      fileUrl='https://storage.googleapis.com/kitchen-sink-sample-images/elephant.jpg',
                      forceRefresh=False)))
        ]
    )
))

# Create the message request
create_request = BusinessmessagesConversationsMessagesCreateRequest(
    businessMessagesMessage=message,
    parent='conversations/' + conversation_id)

# Send the message
bm_client.BusinessmessagesV1.ConversationsMessagesService(
    client=client).Create(request=create_request)

Samples

See the code examples to see example usage for most API features. The samples' README.md has instructions for running the samples.

Sample Source Code
Echo Bot source code

Contributing

Contributions welcome! See the Contributing Guide.

License

Apache Version 2.0

See LICENSE

Metadata

Release files for google-businessmessages 1.0.5

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

Source distribution (sdist)

Source distribution for google-businessmessages 1.0.5
File Size Uploaded Provenance Actions
google-businessmessages-1.0.5.tar.gz 15.7 kB Without provenance Details for google-businessmessages-1.0.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for google-businessmessages 1.0.5
File Interpreter ABI Platform Actions
google_businessmessages-1.0.5-py3-none-any.whl Python 3 none any Details for google_businessmessages-1.0.5-py3-none-any.whl

Total release size: 30.7 kB

Release files / google-businessmessages-1.0.5.tar.gz

Download URL google-businessmessages-1.0.5.tar.gz
Size 15.7 kB
Tags Source
SHA-256 checksum
How to use checksums
101d5618f49de496826901bb51a35ee877e030017241f7210606a2b1d7ed93ba
BLAKE2b-256 checksum
How to use checksums
0d19ae8353b1cc502e086213bf8585f2436fb5b4057268e0b6657b9ded99fb24
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.9

Release files / google_businessmessages-1.0.5-py3-none-any.whl

Download URL google_businessmessages-1.0.5-py3-none-any.whl
Size 15.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f54ccd5d425ebf25783351685d629f33cf3250e3b88cfca185e017d5aa81ad74
BLAKE2b-256 checksum
How to use checksums
40c8c6df5286de5f1a38380a0398964f9e9a6592c80b1dc8b387a44f5a0fb2bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.9

Security for google-businessmessages 1.0.5

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