Skip to content

NCKU-CCS/line_bot_server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mosquito Man Line Bot

Line Bot that provides information about dengue fever.
Including the following functionality

  • Basic Dengue Fever Knowledge
  • Real Time Epidemic Information
  • Nearby Hospital

Add this bot

  • line id: @mosquitoman
  • QR Code QR code

Clone This Project

git clone --recursive https://github.com/NCKU-CCS/line_bot_server

Pre requirements

# mac
brew install postgresql

# ubuntu
sudo apt-get install postgresql postgresql-contrib
# mac
brew install redis

# ubuntu
sudo apt-get install redis-server

Install Dependency

pipenv install

Setup

Use Different Setting Files

Currently this repo tracks only denguefever_tw/denguefever_tw/settings/productions.py
All other setting files should be defined under denguefever_tw/denguefever_tw/settings/

Use the following command to setup or change Django settings

export DJANGO_SETTINGS_MODULE="denguefever_tw.settings.your_setting_file"

Change your_setting_file to corresponding file
e.g. export DJANGO_SETTINGS_MODULE="denguefever_tw.settings.production"

Prodcution

All the sensitive data are not versioned and should be configured by the environment variables.
The variables needed including

  • DJANGO_SECRET_KEY (loaded as SECRET_KEY)
  • LINE_CHANNEL_SECRET
  • LINE_CHANNEL_ACCESS_TOKEN
  • POSTGRESQL_NAME
  • POSTGRESQL_USER
  • POSTGRESQL_PASSWORD
  • POSTGRESQL_HOST
  • POSTGRESQL_PORT
  • IMGUR_CLIENT_ID
  • CHROME_DRIVER_PATH

Develop

When developing, you should define your own local.py under settings/.
This file will be and should be ignored by git.

You should import base.py first.

from .base import *

Then, set up the following variables

  • SECRET_KEY
  • DATABASES
  • LINE_CHANNEL_SECRET
  • LINE_CHANNEL_ACCESS_TOKEN
  • IMGUR_CLIENT_ID
  • CHROME_DRIVER_PATH

e.g.

from .base import *

SECRET_KEY = 'This is the secret key'
LINE_CHANNEL_SECRET = 'This is your Line channel secret'
LINE_CHANNEL_ACCESS_TOKEN = 'This is your Line access token'
IMGUR_CLIENT_ID = 'This is your client id of Imgur application'
CHROME_DRIVER_PATH = 'This is path of your Chrome browser driver'

DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': 'db_name',
        'USER': 'db_user',
        'PASSWORD': 'db_pwd',
        'HOST': 'localhost',
        'PORT': '',
    },
    'tainan': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': 'db_name',
        'USER': 'db_user',
        'PASSWORD': 'db_pwd',
        'HOST': 'localhost',
        'PORT': '',
    },
}

Database

Currently postgis is used with one default db and one tainan db.

  • default: Store all data other than Tainan hospital data
  • tainan: Store Tainan hospital data

Start postgresql

# mac
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

Import Hospital Data

Import tainan hosptial data

python manage.py import_dengue_hospital

Import Tainan Minimum Area Data

python manage.py import_tainan_minarea

Cache

Cache is used to store user state.
Currently redis server is used

Start Redis

redis-server

Logger (Optional But Recommended)

  • loggers
    • django: global logging
    • dengue_linebot.denguebot_fsm: FSM logging including all the conditional judgements and operation processes

e.g.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s:%(asctime)s:%(module)s:%(process)d:%(thread)d\n%(message)s'
        },
        'simple': {
            'format': '%(levelname)s\n%(message)s'
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': 'INFO',
        },
        'dengue_linebot.denguebot_fsm': {
            'handlers': ['console'],
            'level': 'INFO',
        }
    },
}

More detail on Logging System in django

Usage

Run uwsgi

Start

sudo uwsgi --ini server-setting/linebot.ini --touch-reload=`pwd`/server-setting/linebot.ini

Stop

sudo killall -s INT uwsgi

Realod

touch server-setting/linebot.ini

Restart

sudo killall -s INT uwsgi
sudo uwsgi --ini server-setting/linebot.ini --touch-reload=`pwd`/server-setting/linebot.ini

View Log

sudo tail -f /var/log/bot_denguefever_daemon.log

Configration

Under denguefever_tw/denguefever_tw/static/dengue_bot_config

  • FSM.json: Finite state machine
  • dengue_msg.json: Static Messages

Setup callback URL on LINE

Add https://Your Domain Name/callback/ to Webhook URL on your LINE Developer page.

License

Copyright (c) NCKU The Mosquito Man Project. All rights reserved.

Licensed under the MIT License.