facebook-messenger-bot icon indicating copy to clipboard operation
facebook-messenger-bot copied to clipboard

Echo bot

Open AdriG83 opened this issue 7 years ago • 2 comments

Hi, congrats for your tutorial. I write your example

`import os, sys from flask import Flask, request from utils import wit_response from pymessenger import Bot

app = Flask(name)

PAGE_ACCESS_TOKEN="xxx"

bot = Bot(PAGE_ACCESS_TOKEN)

@app.route('/', methods=['GET']) def verify(): # when the endpoint is registered as a webhook, it must echo back # the 'hub.challenge' value it receives in the query arguments if request.args.get("hub.mode") == "subscribe" and request.args.get("hub.challenge"): if not request.args.get("hub.verify_token") == "Magghy": return "Verification token mismatch", 403 return request.args["hub.challenge"], 200

return "Hello world", 200

@app.route('/', methods=['POST']) def webhook(): data = request.get_json() log(data)

if data['object'] == 'page':
	for entry in data['entry']:
		for messaging_event in entry['messaging']:

			# IDs
			sender_id = messaging_event['sender']['id']
			recipient_id = messaging_event['recipient']['id']

			if messaging_event.get('message'):
				# Extracting text message
				if 'text' in messaging_event['message']:
					messaging_text = messaging_event['message']['text']
				else:
					messaging_text = 'no text'

				response = None

				entity, value = wit_response(messaging_text)
				if entity == 'libro':
					response = "Ok, I will send you the {} news".format(str(value))
				elif entity == 'luogo':
					response = "Ok, so you live in {0}. Here are top headlines from {0}".format(str(value))

				if response == None:
					response = "Non so cosa dirti"
					
				bot.send_generic_message(sender_id, response)

return "ok", 200

def log(message): print(message) sys.stdout.flush()

if name == 'main': app.run(debug = True, port = 8000)`

But my bot continues to answer me with the phrases I write to him :(

What is my mistake?

Thanks a lot

AdriG83 avatar Oct 24 '17 17:10 AdriG83

Can you please elaborate your problem? What input are you giving and what is the output?

nikhilkumarsingh avatar Oct 24 '17 18:10 nikhilkumarsingh

Sorry, I solved the problem of echo. I want to create a conversational chat bot not connected with the news. I have to manage all the answers of my bot from app.py with the def webook() or with the "Stories" tab of Wit.ai? The intents Wit of my bot will be many.

AdriG83 avatar Oct 24 '17 18:10 AdriG83