viber_chat_bot

mail@pastecode.io avatar
unknown
python
2 years ago
1.7 kB
1
Indexable
Never
JSON_FILE_PATH = open('D:\\Test Projects\\Scrapy\\scrapers\\movies.json')
data = json.load(JSON_FILE_PATH)
JSON_FILE_PATH.close()

DAYS = []
dates_resp = 'Which day are you interested in?\n'
i = 1
for dict_obj in data:
    for key in dict_obj:
        DAYS.append(key)
        dates_resp = dates_resp + '\n' + (str(i) + '. ' + key)
        i = i + 1


@app.route('/', methods=['POST'])
def incoming():
    # 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 = viber_request.message.text.lower()

        if message == 'dates':
            viber.send_messages(viber_request.sender.id, [
                TextMessage(text=dates_resp)
            ])
        else:
            # lets echo back
            viber.send_messages(viber_request.sender.id, [
                viber_request.message
            ])
    elif isinstance(viber_request, ViberSubscribedRequest):
        viber.send_messages(viber_request.get_user.id, [
            TextMessage(text='Thanks for subscribing!')
        ])
    elif isinstance(viber_request, ViberFailedRequest):
        print()
        # logger.warn("client failed receiving message. failure: {0}".format(viber_request))

    return Response(status=200)


if __name__ == "__main__":
    context = ('server.crt', 'server.key')
    app.run(port='8080')