├── chatbot ├── __init__.py ├── wsgi.py ├── urls.py └── settings.py ├── jokebot ├── __init__.py ├── migrations │ └── __init__.py ├── tests.py ├── admin.py ├── apps.py ├── models.py └── views.py ├── db.sqlite3 ├── README.md └── manage.py /chatbot/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /jokebot/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /jokebot/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Anil-matcha/Facebook-chat-bot/master/db.sqlite3 -------------------------------------------------------------------------------- /jokebot/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /jokebot/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /jokebot/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class JokebotConfig(AppConfig): 5 | name = 'jokebot' 6 | -------------------------------------------------------------------------------- /jokebot/models.py: -------------------------------------------------------------------------------- 1 | from __future__ import unicode_literals 2 | 3 | from django.db import models 4 | 5 | # Create your models here. 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | Facebook chat bot which replies a joke to any message. 3 | 4 | Tutorial here https://codeexperiments.quora.com/Facebook-chat-bot-aka-joke-bot-with-django-tutorial 5 | -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import sys 4 | 5 | if __name__ == "__main__": 6 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "chatbot.settings") 7 | 8 | from django.core.management import execute_from_command_line 9 | 10 | execute_from_command_line(sys.argv) 11 | -------------------------------------------------------------------------------- /chatbot/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for chatbot project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "chatbot.settings") 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /chatbot/urls.py: -------------------------------------------------------------------------------- 1 | """chatbot URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/1.9/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Add an import: from blog import urls as blog_urls 14 | 2. Import the include() function: from django.conf.urls import url, include 15 | 3. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls)) 16 | """ 17 | from django.conf.urls import url, include 18 | from django.contrib import admin 19 | from jokebot.views import jokebot 20 | 21 | urlpatterns = [ 22 | url(r'^admin/', admin.site.urls), 23 | url(r'^james/?$', jokebot.as_view()) 24 | ] 25 | -------------------------------------------------------------------------------- /jokebot/views.py: -------------------------------------------------------------------------------- 1 | import json, requests, random, re 2 | from pprint import pprint 3 | 4 | from django.views import generic 5 | from django.http.response import HttpResponse 6 | 7 | from django.views.decorators.csrf import csrf_exempt 8 | from django.utils.decorators import method_decorator 9 | 10 | 11 | def get_joke(fbid, recevied_message): 12 | joke_text = requests.get("http://api.icndb.com/jokes/random/").json()['value']['joke'] 13 | post_message_url = 'https://graph.facebook.com/v2.6/me/messages?access_token=%s'%'EAAWYSNXVSy4BANk0OrUodmUt8uMvccWZCTwL3qoZAARMzKxZCipcuZBwkSbLG9ZAYbeJukZBNK9E17IxUVqQjA6je8rR1Y8LMaZCGBEHWZCgJpB22Bu3FwgvxPEjZCUIxANJZCjZA6jaTAsiNZBQtEsSDZA5nRsaTAxn8GHKGxgJ39TEiZAQZDZD' 14 | response_msg = json.dumps({"recipient":{"id":fbid}, "message":{"text":joke_text}}) 15 | status = requests.post(post_message_url, headers={"Content-Type": "application/json"},data=response_msg) 16 | pprint(status.json()) 17 | 18 | class jokebot(generic.View): 19 | def get(self, request, *args, **kwargs): 20 | if self.request.GET['hub.verify_token'] == verify_token: 21 | return HttpResponse(self.request.GET['hub.challenge']) 22 | else: 23 | return HttpResponse('Error, invalid token') 24 | 25 | @method_decorator(csrf_exempt) 26 | def dispatch(self, request, *args, **kwargs): 27 | return generic.View.dispatch(self, request, *args, **kwargs) 28 | 29 | 30 | def post(self, request, *args, **kwargs): 31 | incoming_message = json.loads(self.request.body.decode('utf-8')) 32 | for entry in incoming_message['entry']: 33 | for message in entry['messaging']: 34 | if 'message' in message: 35 | get_joke(message['sender']['id'], message['message']['text']) 36 | return HttpResponse() -------------------------------------------------------------------------------- /chatbot/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for chatbot project. 3 | 4 | Generated by 'django-admin startproject' using Django 1.9. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.9/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/1.9/ref/settings/ 11 | """ 12 | 13 | import os 14 | 15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = '=oa+c06^y8@3i317um+ilc0^^w9kq18-rr0e@#+nxf)1!zk+z5' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') 30 | 31 | 32 | # Application definition 33 | 34 | INSTALLED_APPS = [ 35 | 'django.contrib.admin', 36 | 'django.contrib.auth', 37 | 'django.contrib.contenttypes', 38 | 'django.contrib.sessions', 39 | 'django.contrib.messages', 40 | 'django.contrib.staticfiles', 41 | 'jokebot', 42 | ] 43 | 44 | MIDDLEWARE_CLASSES = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 51 | 'django.contrib.messages.middleware.MessageMiddleware', 52 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 53 | ] 54 | 55 | ROOT_URLCONF = 'chatbot.urls' 56 | 57 | TEMPLATES = [ 58 | { 59 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 60 | 'DIRS': [], 61 | 'APP_DIRS': True, 62 | 'OPTIONS': { 63 | 'context_processors': [ 64 | 'django.template.context_processors.debug', 65 | 'django.template.context_processors.request', 66 | 'django.contrib.auth.context_processors.auth', 67 | 'django.contrib.messages.context_processors.messages', 68 | ], 69 | }, 70 | }, 71 | ] 72 | 73 | WSGI_APPLICATION = 'chatbot.wsgi.application' 74 | 75 | 76 | # Database 77 | # https://docs.djangoproject.com/en/1.9/ref/settings/#databases 78 | 79 | DATABASES = { 80 | 'default': { 81 | 'ENGINE': 'django.db.backends.sqlite3', 82 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 83 | } 84 | } 85 | 86 | 87 | # Password validation 88 | # https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators 89 | 90 | AUTH_PASSWORD_VALIDATORS = [ 91 | { 92 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 93 | }, 94 | { 95 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 96 | }, 97 | { 98 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 99 | }, 100 | { 101 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 102 | }, 103 | ] 104 | 105 | 106 | # Internationalization 107 | # https://docs.djangoproject.com/en/1.9/topics/i18n/ 108 | 109 | LANGUAGE_CODE = 'en-us' 110 | 111 | TIME_ZONE = 'UTC' 112 | 113 | USE_I18N = True 114 | 115 | USE_L10N = True 116 | 117 | USE_TZ = True 118 | 119 | 120 | # Static files (CSS, JavaScript, Images) 121 | # https://docs.djangoproject.com/en/1.9/howto/static-files/ 122 | 123 | STATIC_URL = '/static/' 124 | --------------------------------------------------------------------------------