Answer a question

I'm trying to develop a kik bot. I used ngrok to tunnel my localhost to a ngrok server. However, whenever I run my python program and start the ngrok server and message the bot on my phone, all it returns are 404 errors. Here is my python code

from flask import Flask, request, Response
import os 
from kik import KikApi, Configuration 
from kik.messages import messages_from_json, TextMessage

app = Flask(__name__)
BOT_USERNAME = os.environ.get('BOT_USERNAME') 
BOT_API_KEY =  os.environ.get('BOT_API_KEY') 

kik = KikApi(BOT_USERNAME, BOT_API_KEY)

kik.set_configuration(Configuration(webhook='my_webhook'))

@app.route('/incoming', methods=['POST'])
def incoming():
    if not kik.verify_signature(request.headers.get('X-Kik-Signature'),    request.get_data()):
    return Response(status=403) 

    messages = messages_from_json(request.json['messages'])

    for message in messages:
        if isinstance(message, TextMessage):
            kik.send_messages([
                TextMessage(
                    to=message.from_user,
                    chat_id=message.chat_id,
                    body=message.body
                )
            ])

return Response(status=200)


if __name__ == "__main__":
    app.run(port=8080, debug=True)

Basically, when I run this file, ngrok and the localhost tell me "404 not found". I followed the directions here and made a POST to set up my bot's configuration. When I check the kik bot for the webhook, it shows the ngrok url. Is there something else I need to do to be able to send messages to the bot as a normal user? I know the kik authenticates using the "X-Kik-Username", so does that have something to do with it?

Error messages from ngrok

Answers

I figured it out. I changed the route in the code from "/incoming" to "/". That allowed for the right response.

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐