1
votes

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

2
After running this, how are you trying to access it? - lennard
@lennard I just message the bot on my phone - Bob
Can you show the error message your getting please? - lennard
@lennard sure, I added the error message that ngrok is giving me whenever I send a message to the bot on my phone - Bob
Ok so it looks like ngrok is sending a POST to / when your route is /incoming. Try changing it to match. - lennard

2 Answers

1
votes

I had a similar problem using Django with 404s because the webhook URL specified in the external platform was included in the request to local dev server by ngrok, so something like http://xyz.ngrok.io/https://xyz.ngrok.io/api/v1/webhooks was used as request. Using -host-header option solved it for me:

./ngrok http -host-header=rewrite 127.0.0.1:8000
0
votes

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