Package: google-businessmessages
Google's Business Messages: Python ClientBusiness Messagesis a mobile conversational channel that combines entry points on Google Maps, Search, and brand websites to create rich, asynchronous messaging experiences.This document contains anAPI reference, samples, and other resources useful to developing Python applications.
For additional help developing Business Messages applications, in Python and other languages, see ourBusiness Messages quickstartguide.DocumentationThe documentation for the Business Messages API can be foundhere.QuickstartBefore you beginRegister with Business Messages.Once registered, follow the instructions toenable the APIs for your project.InstallationMac/Linuxpython -m venv <your-env>
source <your-env>/bin/activate
<your-env>/bin/pip install google-businessmessagesWindowspython -m venv <your-env>
<your-env>\Scripts\activate
<your-env>\Scripts\pip.exe install google-businessmessagesSupported Python VersionsPython 3.5, 3.6 and 3.7, and 3.8 are fully supported and tested.Using the client libraryfrombusinessmessagesimportbusinessmessages_v1_clientasbm_clientfrombusinessmessages.businessmessages_v1_messagesimport(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 requestcreate_request=BusinessmessagesConversationsMessagesCreateRequest(businessMessagesMessage=message,parent='conversations/'+conversation_id)# Send the messagebm_client.BusinessmessagesV1.ConversationsMessagesService(client=client).Create(request=create_request)Sample usageSamples below assume a similar library initialization as shown in theUsing the client librarysection.Sending a text messagemessage=BusinessMessagesMessage(messageId=str(uuid.uuid4().int),representative=BusinessMessagesRepresentative(representativeType=BusinessMessagesRepresentative.RepresentativeTypeValueValuesEnum.BOT),text='Hello, World!')# Create the message requestcreate_request=BusinessmessagesConversationsMessagesCreateRequest(businessMessagesMessage=message,parent='conversations/'+conversation_id)# Send the messagebm_client.BusinessmessagesV1.ConversationsMessagesService(client=client).Create(request=create_request)Sending a text message with suggested replies and actionsmessage=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 requestcreate_request=BusinessmessagesConversationsMessagesCreateRequest(businessMessagesMessage=message,parent='conversations/'+conversation_id)# Send the messagebm_client.BusinessmessagesV1.ConversationsMessagesService(client=client).Create(request=create_request)Sending a rich cardmessage=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 requestcreate_request=BusinessmessagesConversationsMessagesCreateRequest(businessMessagesMessage=message,parent='conversations/'+conversation_id)# Send the messagebm_client.BusinessmessagesV1.ConversationsMessagesService(client=client).Create(request=create_request)Sending a carouselmessage=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 requestcreate_request=BusinessmessagesConversationsMessagesCreateRequest(businessMessagesMessage=message,parent='conversations/'+conversation_id)# Send the messagebm_client.BusinessmessagesV1.ConversationsMessagesService(client=client).Create(request=create_request)SamplesSee the code examples to see example usage for most API features. The samples'README.mdhas instructions for running the samples.SampleSource CodeEcho Botsource codeContributingContributions welcome! See theContributing Guide.LicenseApache Version 2.0SeeLICENSE