nael_d / viberify
Viberify simplifies sending messages (text, images, videos, files, contacts, locations, URLs, and stickers), managing webhooks, and retrieving account information for your Viber Channel.
Requires
- php: >=7.0.0
README
Viberify is a simple PHP library for interacting with the Viber Channel API, allowing you to send messages, media, contacts, URLs, locations, and more, to your Viber Channel.
Features
- PHP Compatibility: Works with PHP >= 7.0.0 minimum.
- Easy Authentication: Set up your Viber Channel's authentication token and Viber ID to start interacting with the Viber API.
- Webhook Integration: Set a secure webhook URL to receive callbacks and user messages.
- Account Info Retrieval: Fetch details about your Viber Channel's account, including editable fields from the Viber app.
- Error Handling: Includes built-in error handling for issues with Viber API responses.
- Multiple Message Types: Send a variety of message types, including:
- Text messages (up to 7000 characters).
- Image messages (JPEG format, up to 1MB).
- Video messages (MP4/H264 format, up to 50MB).
- File messages (up to 50MB with proper extensions).
- Contact card information.
- Location coordinates (via Google Maps).
- URL links with optional descriptions.
- Stickers (by ID).
Table of Contents
Prerequisites
- Viber Channel: Only superadmins of a Viber Channel can access the API.
- Viber Version: Ensure you're using Viber version 17.7 or higher.
- Authentication Token: Obtain the authentication token for your Viber Channel from the Viber Developer Tools page.
Installation
Viberify is available to be installed through Composer:
composer require nael_d/viberify
You are also able to download Viberify manually. Download Viberify and require src/Viberify.php
.
For a better release checking, we may suggest to review Releases page to pick up and review developing timeline, as it provides a clear listing of versions along with detailed descriptions of each release.
Getting Started
Once installed, the class Viberify
is ready for you.
💡
Viberify
class is available underViberify
namespace.
The first thing that you need before you go on, you have to obtain the Authentication token key of your targeted channel:
Then copy the Authentication token:
API Methods
Viberify provides a set of methods to interact with the Viber Channel API, allowing you to send various types of content and manage your channel. Below is an overview of the available methods:
set_auth_token:
Once copied your Authentication token, initialize Viberify with your token to get started:
\Viberify\Viberify::set_auth_token("VIBER_AUTH_TOKEN");
It's a good practice to store it in an
.env
file for a better flexible managing:\Viberify\Viberify::set_auth_token(env('VIBER_AUTH_TOKEN'));
set_webhook:
Sets the webhook URL for your Viber Channel. The webhook will be used to receive callbacks and messages from users.
Parameters:
string $webhook
: The webhook URL (must use HTTPS).
Usage:
\Viberify\Viberify::set_webhook('https://your-webhook-url.com/target');
💡 You only need to call this method once. No need to recall it each time.
💡 For more info about Viber Webhooks, refer to Setting a Webhook page to know more.
get_account_info:
Fetches the details of your Viber channel as registered with Viber. This helps you to get your Viber unique ID.
Usage:
$account_info = \Viberify\Viberify::get_account_info(); $viber_id = $account_info['members'][0]['id']; /** * array(6) { ["status"]=> int(0) ["status_message"]=> string(2) "ok" ["id"]=> string(22) "pa:0000000000000000000" ["chat_hostname"]=> string(11) "SN-CHAT-01_" ["name"]=> string(12) "Demo Channel" ["members"]=> array(1) { [0]=> array(4) { ["id"]=> string(24) "QerXFUgfdI45gfdBdfMZKq==" ["name"]=> string(11) "Viber's account name" ["avatar"]=> string(208) "https://media-direct.cdn.viber.com/download_photo?fltp=jpg" ["role"]=> string(10) "superadmin" } } } */
💡 It's a better practise to copy your account's
id
rather than call this method to gain it repeatedly each time you perform an action to avoid duplicated requests.💡 You only need to call this method once you've prepared Viberify to get your account's
id
. No need to call it later when unneeded.set_viber_id:
Sets the Viber ID representing the Viber account that will appear as the sender.
Parameters:
string $viber_id
: The Viber ID of the superadmin account gained fromget_account_info()
.
Usage:
\Viberify\Viberify::set_viber_id('YOUR_VIBER_ID');
send_message:
Send a textual message to the Viber Channel.
Parameters:
string $message
: Required. The message text (max 7000 characters).
Usage:
\Viberify\Viberify::send_message('Hello, Viber!');
send_image:
Send a picture message to the Viber Channel.
Parameters:
string $image
: Required, max size 1MB. URL of the image (JPEG). Only JPEG format is supported. The URL must have a resource with a.jpeg
file extension.string $message
: Optional, max 768 characters. Description of the photo.string $thumb
: Optional, max size 100kb, Recommended dimensions is 400x400. Only JPEG format is supported.
Usage:
\Viberify\Viberify::send_image( 'https://example.com/image.jpeg', 'Here is an image!', 'https://example.com/thumb.jpeg' );
send_video:
Send a video message to the Viber Channel.
Parameters:
string $video
: Required, max size 50MB. OnlyMP4
andH264
are supported.float $size
: Required, in MBs. Size of the video in MB.int $duration
: Optional, max 180 seconds. Video duration in seconds; will be displayed to the receiver.string $message
: Optional, max 768 characters. Description of the video.string $thumb
: Optional, max size 100kb, Recommended dimensions is 400x400. URL of a reduced size image (JPEG
). Only JPEG format is supported.
Usage:
\Viberify\Viberify::send_video( 'https://example.com/video.mp4', 15, 120, 'Video description', 'https://example.com/thumb.jpeg' );
send_file:
Send a file message to the Viber Channel.
Parameters:
string $file
: Required, max size 50MB. See forbidden file formats for unsupported file types.float $size
: Required, in MBs. Size of the file in MB.string $filename
: Required, max 256 characters (including file extension). File name should include extension. Sending a file without extension or with the wrong extension might cause the client to be unable to open the file.
Usage:
\Viberify\Viberify::send_file('https://example.com/file.zip', 25.81, 'file.zip');
send_contact:
Send a contact message to the Viber Channel.
Parameters:
string $name
: Required, max 28 characters. Name of the contact.string $phone_number
: Required, max 18 characters. Phone number of the contact.
Usage:
\Viberify\Viberify::send_contact('John Doe', '+123456789012345');
send_location:
Send a Google Maps location message to the Viber Channel.
Parameters:
string $lat
: Required, range between ±90°. Location coordinates.string $lon
: Required, range between ±180°. Location coordinates.
Usage:
// San Francisco coordinates \Viberify\Viberify::send_location('37.7749', '-122.4194');
send_url:
Send a URL message to the Viber Channel.
Parameters:
string $link
: Required, max 2,000 characters. URL link.string $message
: Optional, max 120 characters. Description of the URL.
Usage:
\Viberify\Viberify::send_url('https://example.com', 'Check out this link!');
send_sticker:
Send a sticker message. See full list of stickers IDs.
Parameters:
string $sticker_id
: Required. Unique Viber sticker ID.
Usage:
\Viberify\Viberify::send_sticker(46105);
Error Handling
If there is an issue with the request, the library will return a status code and message indicating the error. For more details on error codes, refer to the Viber API Error Codes page.
Notes
- Forbidden File Formats: Viber restricts certain file formats from being uploaded. Refer to the forbidden file formats documentation for a full list of unsupported file types.
- SSL Requirements: The webhook URL must use HTTPS with a valid SSL certificate. Viber does not support self-signed certificates.
Conclusion
The main purpose of Viberify is to simplify the process of publishing content to your Viber Channel, allowing you to automate message delivery to all your subscribers directly from your website.
Whether you're sharing text updates, images, videos, or files, Viberify makes it easy to programmatically engage with your Viber audience at scale.
By integrating this library, you can streamline content distribution and communication, ensuring seamless interaction with your channel’s subscribers.
License
This project is licensed under the Mozilla Public License 2.0 (MPL-2.0). You are free to use, modify, and distribute this library under the terms of the MPL-2.0. See the LICENSE file for details.