Viber Bot does not start

Create a .pdf file and store it in a local path such as “ngrokpath/myfiles/mypdf.pdf”. Next, utilize the “expose_url” which is the Ngrok session URL, along with “pdffilesize” for reference according to the Viber API in order to use it in “conversation_started” or “message_received” events within your bot. In case you need to obtain user details, refer to the endpoint specified in the Viber NodeJS developer documentation.


Question:

My Viber bot is operated via

gunicorn

and

flask

on the server. However, I encounter an error when making a

curl

request.

* Curl_http_done: called premature == 0
* Connection #0 to host chatapi.viber.com left intact
{"status":1,"status_message":"Result[HttpRequest[POST / HTTP/1.1]@3f742f36 > HttpResponse[HTTP/1.1 500 INTERNAL SERVER ERROR]@16f0ed02] null","chat_hostname":"SN-CHAT-01_"}

when I open url my site I have

Method Not Allowed
The method is not allowed for the requested URL.


app.py

from flask import Flask, request, Response
from viberbot import Api
from viberbot.api.bot_configuration import BotConfiguration
from viberbot.api.messages import KeyboardMessage
from viberbot.api.messages.text_message import TextMessage
import logging
from viberbot.api.viber_requests import ViberFailedRequest
from viberbot.api.viber_requests import ViberMessageRequest
from viberbot.api.viber_requests import ViberSubscribedRequest
from keyboard import keyboard_menu
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
app = Flask(__name__)
viber = Api(BotConfiguration(
    name='name',
    avatar='https://upload.wikimedia.org/wikipedia/commons/9/9a/Gull_portrait_ca_usa.jpg',
    auth_token='token'
))
@app.route('/', methods=['POST'])
def incoming():
    logger.debug("received request. post data: {0}".format(request.get_data()))
    # every viber message is signed, you can verify the signature using this method
    if not viber.verify_signature(request.get_data(), request.headers.get('X-Viber-Content-Signature')):
        return Response(status=403)
    # this library supplies a simple way to receive a request object
    viber_request = viber.parse_request(request.get_data())
    if isinstance(viber_request, ViberMessageRequest):
        message = TextMessage(text="test", keyboard=keyboard_menu)
        # lets echo back
        viber.send_messages(viber_request.sender.id, [
            message
        ])
    elif isinstance(viber_request, ViberSubscribedRequest):
        viber.send_messages(viber_request.get_user.id, [
            TextMessage(text="thanks for subscribing!")
        ])
    elif isinstance(viber_request, ViberFailedRequest):
        logger.warn("client failed receiving message. failure: {0}".format(viber_request))
    return Response(status=200)


wsgi.py

from app import app
if __name__ == "__main__":
    app.run()


curl

curl -# -i -g -H "X-Viber-Auth-Token:token" -d @viber.json -X POST https://chatapi.viber.com/pa/set_webhook -v


viber.json

{
  "url": "https://my.site.com"
}

What am I doing wrong?


Solution:

Attempt to send a POST request to the specified endpoint utilizing

cURL

in the same manner as the Viber server to observe any errors that may occur.

Suggest configuring the root logger as per the documentation to view the cause of the 500 error. Further details can be found here.

from logging.config import dictConfig
dictConfig({
    'version': 1,
    'formatters': {'default': {
        'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
    }},
    'handlers': {'wsgi': {
        'class': 'logging.StreamHandler',
        'stream': 'ext://flask.logging.wsgi_errors_stream',
        'formatter': 'default'
    }},
    'root': {
        'level': 'INFO',
        'handlers': ['wsgi']
    }
})
app = Flask(__name__)

And the last think:

when I open url my site I have

Method Not Allowed
The method is not allowed for the requested URL.

The reason for this is that the URL can only be accessed through the HTTP method, but when opened in a browser, a GET request is sent which is not accepted by the endpoints.

Frequently Asked Questions