How to create a custom connector in rasa for Viber connectivity

489 Views Asked by At

How to create a custom connector in rasa for Viber connectivity

• This is my current custom connector file. the file name is viber.py and I am using rasa 2.8 ( I had to hide the hostname in webhook)

from http.client import HTTPResponse
from viberbot import Api
from viberbot.api.bot_configuration import BotConfiguration
from typing import Text, List, Dict, Any, Optional, Callable, Iterable, Awaitable
from asyncio import Queue
from sanic.request import Request
from rasa.core.channels import InputChannel
from rasa.core.agent import Agent
from rasa.core.channels.channel import UserMessage, CollectingOutputChannel, QueueOutputChannel
from rasa.utils.endpoints import EndpointConfig
from rasa import utils
from flask import Blueprint, request, jsonify
from sanic import Blueprint, response
from rasa.model import get_model, get_model_subdirectories
import inspect
from rasa.core.run import configure_app

bot_configuration = BotConfiguration(
    name='Rasa_demo_one',
    avatar='',
    auth_token='4f831f01ef34ad38-9d057b2fd4ba804-8dd0cf1cdf5e39dc'
)
viber = Api(bot_configuration)
viber.set_webhook('<host doamin>/webhooks/viber/webhook')

class ViberInput(InputChannel):
    """Viber input channel implementation. Based on the HTTPInputChannel."""

    @classmethod
    def name(cls) -> Text:
        return "viber"

    @classmethod
    def from_credentials(cls, credentials: Optional[Dict[Text, Any]]) -> InputChannel:
        if not credentials:
            cls.raise_missing_credentials_exception()

        return cls(
            credentials.get("viber_name"),
            credentials.get("avatar"),
            credentials.get("auth_token")
            )

    def __init__(self, viber_name: Text, avatar: Text, auth_token: Text) -> None:
        """Create a Viber input channel.
        """
        self.viber_name = viber_name
        self.avatar = avatar
        self.auth_token = auth_token
        self.viber = Api(
            BotConfiguration(
                name=self.viber_name,
                avatar=self.avatar,
                auth_token=self.auth_token
                )
        )

    def blueprint(self, on_new_message: Callable[[UserMessage], Awaitable[Any]]) -> Blueprint:
        viber_webhook = Blueprint("viber_webhook", __name__)

        @viber_webhook.route("/", methods=["POST"])
        async def incoming(request: Request) -> HTTPResponse:
            viber_request = self.viber.parse_request(request.get_data())
            if isinstance(viber_request):
                message = viber_request.message
                # lets echo back
                self.viber.send_messages(viber_request.sender.id, [
                    message
                ])
            return response.text("success")

        return viber_webhook

• I created this file where the credentials file is located.

• This is the credential I put in the credentials.yml file

viber.ViberInput:
 viber_name: "Rasa_demo_one"
 avatar: ""
 auth_token: "4f831f01ef34ad38-9d057b2fd4ba804-8dd0cf1cdf5e39dc"

• when I tries to run rasa with this configuration I got an error that says

RasaException: Failed to find input channel class for 'Viber.ViberInput'. Unknown input channel. Check your credentials configuration to make sure the mentioned channel is not misspelled. If you are creating your own channel, make sure it is a proper name of a class in a module.
2

There are 2 best solutions below

0
On

You didn't tell about path of you file and filename itself.. If it's your correct credentials when your file must be located in rasa's root directory with viber.py name.

And looks like you forgot to import InputChannel

from rasa.core.channels.channel import InputChannel, UserMessage, CollectingOutputChannel, QueueOutputChannel
0
On

always check:

  1. that the custom channel path is correct
  2. if the channel is inside a directory, make sure there is a __init__.py file to make sure its a module
  3. the most tricky: have module requirements installed. If you import a package inside your custom channel that does not exist, the error will be a path error!