Use this library to develop a bot for Viber platform. The library is available on GitHub as well as a package on PyPI.
This package can be imported using pip by adding the following to your requirements.txt
:
This library is released under the terms of the Apache 2.0 license. See License for more information.
Creating a basic Viber bot is simple:
pip install viberbot
viberbot.api
library to your projectset_webhook(url)
with your web server urlNext thing you should do is starting a https server.
and yes, as we said in the prerequisites it has to be https server. Create a server however you like, for example with Flask
:
After the server is up and running you can set a webhook. Viber will push messages sent to this URL. web server should be internet-facing.
This library uses the standard python logger. If you want to see its logs you can configure the logger:
Well, funny you ask. Yes we do. All the Message types are located in viberbot.api.messages
package. Here’s some examples:
Have you noticed how we created the TextMessage
? There’s a all bunch of message types you should get familiar with.
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.
As you can see there’s a bunch of Request
types here’s a list of them.
from viberbot import Api
List of registered event_types
None
object
boolean
ViberRequest
list of message tokens sent
dictionary of users status
dictionary of user's data
list of message tokens sent
Param | Type | Description |
---|---|---|
bot_configuration | object |
BotConfiguration |
Param | Type | Description |
---|---|---|
url | string |
Your web server url |
webhook_events | list |
optional list of subscribed events |
Returns List of registered event_types
.
Returns None
.
Returns an object
with the following JSON.
Param | Type | Description |
---|---|---|
request_data | string |
the post data from request |
signature | string |
sent as header X-Viber-Content-Signature |
Returns a boolean
suggesting if the signature is valid.
Param | Type | Description |
---|---|---|
request_data | string |
the post data from request |
Returns a ViberRequest
object.
There’s a list of ViberRequest objects
Param | Type | Description |
---|---|---|
to | string |
Viber user id |
messages | list |
list of Message objects |
Returns list
of message tokens of the messages sent.
Param | Type | Description |
---|---|---|
sender | string |
Viber user id |
messages | list |
list of Message objects |
Returns list
of message tokens of the messages sent.
Param | Type | Description |
---|---|---|
viber_user_ids | array of strings |
Array of Viber user ids |
Returns a dictionary of users
.
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 account regarding user’s actions. This request can be sent twice during a 12 hours period for each user ID.
Param | Type | Notes |
---|---|---|
event_type | string |
according to EventTypes enum |
timestamp | long |
Epoch of request time |
string
long
Inherits from ViberRequest
Conversation started event fires when a user opens a conversation with the Public Account/ bot using the “message” button (found on the account’s info screen) or using a deep link.
This event is not considered a subscribe event and doesn’t allow the account 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 |
user | UserProfile |
the user started the conversation UserProfile |
subscribed | boolean |
Indicates whether a user is already subscribed |
string
string
string
UserProfile
Inherits from ViberRequest
Param | Type | Notes |
---|---|---|
event_type | string |
always equals to the value of EventType.DELIVERED |
message_token | string |
Unique ID of the message |
user_id | string |
Unique Viber user id |
string
string
Inherits from ViberRequest
Param | Type | Notes |
---|---|---|
event_type | string |
always equals to the value of EventType.FAILED |
message_token | string |
Unique ID of the message |
user_id | string |
Unique Viber user id |
desc | string |
Failure description |
string
string
string
Inherits from ViberRequest
Param | Type | Notes |
---|---|---|
event_type | string |
always equals to the value of EventType.MESSAGE |
message_token | string |
Unique ID of the message |
message | Message |
Message object |
sender | UserProfile |
the user started the conversation UserProfile |
string
Message
UserProfile
Inherits from ViberRequest
Param | Type | Notes |
---|---|---|
event_type | string |
always equals to the value of EventType.SEEN |
message_token | string |
Unique ID of the message |
user_id | string |
Unique Viber user id |
string
string
Inherits from ViberRequest
Param | Type | Notes |
---|---|---|
event_type | string |
always equals to the value of EventType.SUBSCRIBED |
user | UserProfile |
the user started the conversation UserProfile |
UserProfile
Inherits from ViberRequest
Param | Type | Notes |
---|---|---|
event_type | string |
always equals to the value of EventType.UNSUBSCRIBED |
user_id | string |
Unique Viber user id |
string
Param | Type | Notes |
---|---|---|
id | string |
— |
name | string |
— |
avatar | string |
Avatar URL |
country | string |
currently set in CONVERSATION_STARTED event only |
language | string |
currently set in CONVERSATION_STARTED event only |
Common Members for Message
interface:
Param | Type | Description |
---|---|---|
timestamp | long |
Epoch time |
keyboard | JSON |
keyboard JSON |
trackingData | JSON |
JSON Tracking Data from Viber Client |
Common Constructor Arguments Message
interface:
Param | Type | Description |
---|---|---|
optionalKeyboard | JSON |
Writing Custom Keyboards |
optionalTrackingData | JSON |
Data to be saved on Viber Client device, and sent back each time message is received |
Member | Type |
---|---|
text | string |
Member | Type | Description |
---|---|---|
media | string |
URL string |
Member | Type |
---|---|
contact | Contact |
Member | Type | Description |
---|---|---|
media | string |
url of the message (jpeg only) |
text | string |
|
thumbnail | string |
Member | Type | Description |
---|---|---|
media | string |
url of the video |
size | int |
|
thumbnail | string |
|
duration | int |
Member | Type |
---|---|
location | Location |
Member | Type |
---|---|
sticker_id | int |
Member | Type |
---|---|
media | string |
size | long |
file_name | string |
Member | Type |
---|---|
rich_media | string (JSON) |
Member | Type |
---|---|
keyboard | JSON |
tracking_data | JSON |
The Viber API allows sending messages to users only after they subscribe to the account. However, Viber will allow the account 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 account. To learn more about this event and when is it triggered see Conversation started
in the callbacks section.
Sending a welcome message will be done according to the following flow:
conversation_started
even to PA’s webhook.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:
Join the conversation on Gitter.