Viber REST API Wrapper written in Elixir
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
config webhook server and simple wrapper Nov 10, 2018
lib separate server to plug Nov 19, 2018
test webhook server and simple wrapper Nov 10, 2018
.formatter.exs webhook server and simple wrapper Nov 10, 2018
.gitignore webhook server and simple wrapper Nov 10, 2018
README.md update readme Dec 5, 2018
mix.exs 0.2.1 Nov 20, 2018
mix.lock upgrade plug_cowboy Nov 20, 2018

README.md

Viberex

Hex.pm API Docs

Viber REST API wrapper in Elixir

Installation

Viberex can be installed by adding viberex to your list of dependencies in mix.exs:

def deps do
  [
    {:viberex, "~> 0.2.1"}
  ]
end

Configuration

In config\config.exs, add your Viber authentication token. This token you can take from Viber Admin Panel.

config :viberex,
  auth_token: "token"

Usage

Viber doesn’t provide long-polling for getting updates, instead, Viber uses webhooks and callbacks. Since a webhook requires a public server, you can use ngrok to publish a local server.

Viberex has a behavior module Viberex.Server for implementing the webhook server. By using it, you can handle callbacks from Viber. In your module, you need to define a function with a name handle_callback/1. This function must return {:reply, message} if the callback waits for a response (e.g. welcome message), and :noreply if it doesn’t.

defmodule MyApp.Handler do
  use Viberex.Server

  def handle_callback(%{"event" => "conversation_started"}) do
    welcome_message = %{
      text: "Hi!",
      type: "text"
    }
    {:reply, welcome_message}
  end

  def handle_callback(_), do: :noreply
end

Add server to application’s supervisor tree with endpoint path and port:

defmodule MyApp.Application do
  use Application

  def start(_type, _args) do
    import Supervisor.Spec

    children = [
      worker(MyApp.Handler, ["/webhook/viber", 8000])
    ]

    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

After starting the project, just set webhook's url

$ iex -S mix
iex(1)> Viberex.set_webhook("https://my-url.com/webhook/viber")

All avaible API methods you can find here

Using with Phoenix

Viberex has a plug that you can use in your Phoenix project. Add forward in your router and just define MyPhoenix.Handler module with handle_callback/1 function

defmodule MyPhoenixWeb.Router do
  use MyPhoenixWeb, :router

  forward "webhook/viber",
    Viberex.Plug,
    handler: &MyPhoenix.Handler.handle_callback/1
end

Refer to viberex documentation and Viber API documentation for more details