Google's Business Messages API client library
Project description
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
- Register with Business Messages.
- 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)
Sending a carousel
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
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
Built Distribution
File details
Details for the file google-businessmessages-1.0.5.tar.gz
.
File metadata
- Download URL: google-businessmessages-1.0.5.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 101d5618f49de496826901bb51a35ee877e030017241f7210606a2b1d7ed93ba |
|
MD5 | c0aca9956d382695b1c2cd295f0ff24f |
|
BLAKE2b-256 | 0d19ae8353b1cc502e086213bf8585f2436fb5b4057268e0b6657b9ded99fb24 |
File details
Details for the file google_businessmessages-1.0.5-py3-none-any.whl
.
File metadata
- Download URL: google_businessmessages-1.0.5-py3-none-any.whl
- Upload date:
- Size: 15.0 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f54ccd5d425ebf25783351685d629f33cf3250e3b88cfca185e017d5aa81ad74 |
|
MD5 | 371bd77f13f059ff2b8b1afb7396cdf0 |
|
BLAKE2b-256 | 40c8c6df5286de5f1a38380a0398964f9e9a6592c80b1dc8b387a44f5a0fb2bf |