Viber bot api php

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Provides framework for easy creation of Viber bots

License

avplab/viber-api

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

The ViberApi gives an ability to create fully functional php bots for Viber, based on Viber REST API.

Install the component by using Composer. Update your project’s composer.json file to include dependency.

To create any bot, the package provides two base classes Callback\Request and Client .

Run the following script once to register your webhook(bot). It could be run locally.

#register.php namespace EchoBot; use AvpLab\ViberApi\Callback\Request; use AvpLab\ViberApi\Client; $token = ''; $name = 'Echo Bot'; $url = 'https://webhook.url'; $client = new Client($token, $name); $client->setWebhook( $url, [ Request::DELIVERED, Request::SEEN, Request::FAILED, Request::CONVERSATION_STARTED, Request::SUBSCRIBED, Request::UNSUBSCRIBED, Request::WEBHOOK, Request::MESSAGE ] ); 

Once you are done with registration you can write the code of the bot

namespace EchoBot; use AvpLab\ViberApi\Client; use AvpLab\ViberApi\Callback\Request; use AvpLab\ViberApi\Message\TextMessage; /** * The Echo bot will reply with the same message it received */ $request = new Request(); $client = new Client('', 'Echo Bot'); // when user starts the conversation or was subscribed (by deep link) if ($request->isConversationStarted()) < if ($request->getData()->subscribed) < // user is already subsribed on the bot $message = new TextMessage('Welcome back !'); >else < // new user $message = new TextMessage('Welcome ! I will respond with the same message'); >// response with welcome message $client->responseWelcomeMessage($message); > // User sent the text message to the bot if ($request->isMessageText()) < // response to the user(sender) with the same message as received $client->sendMessage($request->getMessageSenderId(), new TextMessage($request->getMessageText())); > 

The Request provides all the information about callback-request from the Viber API to your server. You can trust the request data, as it is verified for authenticity (see X-Viber-Content-Signature). If for some reason the request cannot be processed, a BadRequestException will be thrown.

To send requests to the API, the Client object is used(the request is sent based on cURL). If for some reason the request does not reach the API, a ServerErrorResponseException exception will be thrown. If the API received the request, but for some reason responded with an error (see API errors), an ApiException exception will be thrown.

The API is using term «message» as request body. Message is a JSON which has predefined structure(see the API docs). Viber describes several types of messages: text , picture , video , contact , rich-media , file , sticker , location and url . To prepare the specific message for sending to the API, you have to create an object of one of the predefined classes:

  • TextMessage
  • PictureMessage
  • VideoMessage
  • ContactMessage
  • RichMediaMessage
  • FileMessage
  • StickerMessage
  • UrlMessage

Each message may contain a keyboard. To add the keyboard use the Keyboard objects(see Keyboard methods for details).

The request object which contains all the info sent by Viber API

  • isWebhook() — the callback was sent on webhook event
  • isSubscribed() — the callback was sent on subscribed event
  • isUnsubsribed() — the callback was sent on unsubscribed event
  • isConversationStarted() — the callback was sent on conversation_started event
  • isDelivered() — the callback was sent on delivered event
  • isSeen() the — callback was sent on seen event
  • isFailed() — the callback was sent on failed event
  • isMessage() — the callback was sent on message event. User sent a message to Public Account(aka PA)
  • isMessageText() — the text message was sent to PA
  • isMessagePicture() — the picture message was sent to PA
  • isMessageVideo() — the video message was sent to PA
  • isMessageFile() — the file message was sent to PA
  • isMessageSticker() — the sticker message was sent to PA
  • isMessageUrl() — the url message was sent to PA
  • isMessageLocation() — the location message was sent to PA
  • isMessageContact() — the contact message was sent to PA
  • getEvent() — returns the callback event name, which triggered the callback
  • getTimestamp() — returns the time of event
  • getMessageToken() — returns Unique ID of the message
  • getMessage() — returns the message data
  • getMessageText() — returns the message.text string
  • getMessageTrackingData() — returns the message.tracking_data string
  • getMessageSender() — returns the sender data of the message
  • getMessageSenderId() — returns the sender.id
  • getConversationContext() — returns the context string of the callback triggered by convestation_started event. Any additional parameters added to the deep link used to access the conversation passed as a string.
  • getConversationUser() — returns the user’s data, who triggered the conversation.
  • getData() — returns the callback request data

The http client which communicates with Viber API.

Оцените статью