License

This library is released under the terms of the Apache 2.0 license. See License for more information.

Library Prerequisites

python >= 2.7.0

Get your Viber Public Account authentication token. Your token is generated and provided to you during the Public Account creation process. As a Public Account admin, you can always find the account token in the “edit info” page.

Certification - You’ll need a trusted (ca.pem) certificate, not self-signed. You can find one at Let’s Encrypt or buy one.

Creating them is easy! Every message object has it’s own unique constructor corresponding to it’s API implementation, click on them to see it!
Check out the full API documentation for more advanced uses.

Let’s add it all up and reply with a message!

fromflaskimportFlask,request,ResponsefromviberbotimportApifromviberbot.api.bot_configurationimportBotConfigurationfromviberbot.api.messagesimportVideoMessagefromviberbot.api.messages.text_messageimportTextMessageimportloggingfromviberbot.api.viber_requestsimportViberConversationStartedRequestfromviberbot.api.viber_requestsimportViberFailedRequestfromviberbot.api.viber_requestsimportViberMessageRequestfromviberbot.api.viber_requestsimportViberSubscribedRequestfromviberbot.api.viber_requestsimportViberUnsubscribedRequestapp=Flask(__name__)viber=Api(BotConfiguration(name='PythonSampleBot',avatar='http://site.com/avatar.jpg',auth_token='445da6az1s345z78-dazcczb2542zv51a-e0vc5fva17480im9'))@app.route('/',methods=['POST'])defincoming():logger.debug("received request. post data: {0}".format(request.get_data()))# every viber message is signed, you can verify the signature using this methodifnotviber.verify_signature(request.get_data(),request.headers.get('X-Viber-Content-Signature')):returnResponse(status=403)# this library supplies a simple way to receive a request objectviber_request=viber.parse_request(request.get_data())ifisinstance(viber_request,ViberMessageRequest):message=viber_request.message# lets echo backviber.send_messages(viber_request.sender.id,[message])elifisinstance(viber_request,ViberSubscribedRequest):viber.send_messages(viber_request.get_user.id,[TextMessage(text="thanks for subscribing!")])elifisinstance(viber_request,ViberFailedRequest):logger.warn("client failed receiving message. failure: {0}".format(viber_request))returnResponse(status=200)if__name__=="__main__":context=('server.crt','server.key')app.run(host='0.0.0.0',port=443,debug=True,ssl_context=context)

As you can see there’s a bunch of Request types here’s a list of them.

Api.get_online(viber_user_ids)

Api.get_user_details(viber_user_id)

Param

Type

Description

viber_user_ids

string

Viber user id

The get_user_details function will fetch the details of a specific Viber user based on his unique user ID. The user ID can be obtained from the callbacks sent to the PA regrading user’s actions. This request can be sent twice during a 12 hours period for each user ID.

user_data=Api.get_user_details("userId")

Request object

Param

Type

Notes

event_type

string

according to EventTypes enum

timestamp

long

Epoch of request time

ViberRequest

.event_type ⇒ string

.timestamp ⇒ long

ViberConversationStartedRequest object

Conversation started event fires when a user opens a conversation with the PA using the “message” button (found on the PA’s info screen) or using a deep link.

This event is not considered a subscribe event and doesn’t allow the PA to send messages to the user; however, it will allow sending one “welcome message” to the user. See sending a welcome message below for more information.

Param

Type

Notes

event_type

string

always equals to the value of EventType.CONVERSATION_STARTED

message_token

string

Unique ID of the message

type

string

The specific type of conversation_started event.

context

string

Any additional parameters added to the deep link used to access the conversation passed as a string

KeyboardMessage object

Sending a welcome message

The Public Accounts API allows sending messages to users only after they subscribe to the PA. However, Viber will allow the PA to send one “welcome message” to a user as the user opens the conversation, before the user subscribes.

The welcome message will be sent as a response to a conversation_started callback, which will be received from Viber once the user opens the conversation with the Public Account. To learn more about this event and when is it triggered see Conversation started in the Callbacks section.

Welcome message flow

Sending a welcome message will be done according to the following flow:

User opens 1-on-1 conversation with PA.

Viber server send “conversation_started” even to PA’s webhook.

PA receives the “conversation_started” and responds with an HTTP response which includes the welcome message as the response body.

The welcome message will be a JSON constructed according to the send_message requests structure, but without the receiver parameter. An example welcome message would look like this: