saneery / viberex

Viber REST API Wrapper written in Elixir

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

Viber REST API Wrapper written in Elixir


Languages

Language:Elixir 100.0%