Created
August 20, 2016 11:41
-
-
Save sido378/4347a34260f3def303c37f0b4769ba67 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import json, urllib | |
from flask import Flask, request, abort | |
import requests | |
from pymessenger.bot import Bot | |
from pprint import pprint | |
DB_FILE = "data.json" | |
access_token = 'ACCESS_TOKEN' | |
bot = Bot(access_token) | |
with open('data.json') as data_file: | |
user_db = json.load(data_file) | |
@app.route('/webhook', methods=['POST']) | |
def post_webhook(): | |
data = request.json | |
if data["object"] == "page": | |
for entry in data['entry']: | |
for messaging_event in entry['messaging']: | |
if "message" in messaging_event: | |
sender_id = messaging_event['sender']['id'] | |
# writing and actually persisting the db | |
if sender_id not in user_db: | |
user_db[sender_id] = {} | |
save_db() | |
if 'text' in messaging_event['message']: | |
message_text = messaging_event['message']['text'] | |
bot.send_text_message(sender_id, message_text) | |
return "ok", 200 | |
def save_db(): | |
with open('data.json', 'w') as data_file: | |
json.dump(user_db, data_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment