Skip to content

hunj/yui

 
 

Repository files navigation

YUI

CI Test Status

Code Coverage Status

Requirements Status

YUI is Multi-purpose Slack Bot.

Who do use YUI?

  • item4(owner)'s private slack
  • 9xd
  • xnuk fan club
  • PyJog

Requirements

  • Git
  • Slack bot permission for bot account
  • Python 3.7 or higher
  • Poetry

Installation

If you did not have Poetry, Install it by below command.

$ curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python
# or see https://poetry.eustace.io/docs/#installation for details

You can install yui by poetry.

$ git clone https://github.com/item4/yui.git
$ cd yui
$ mkdir log
$ poetry install --no-dev

Configuration

Yui must require ~~~.config.toml file for run. You must make config file before run.

Config key and value is below.

TOKEN

string. Slack App Toekn

PREFIX

string. Prefix for command. for example, if you set PREFIX to '=' and you want to run help command, you must type =help

RECEIVE_TIMEOUT

integer. timeout seconds for receiving data from Slask WebSocket. default is 300 (5min)

APPS

list of str. Python module path of apps. Yui import given paths automatically. You must use core apps.

HANDLERS = [
    'yui.apps.core'
]
CHANNELS

dictionary of str. Channel names used in code. it used for support same handler code with different server envrionment.

For example,

[CHANNELS]
general = '_general'
do_not_use_gif = ['dev', '_notice']

This config setting make you use yui.command.C and yui.command.Cs like this.

@box.comamnd('only-general', channel=only(C.general))
async def only_general():
   # this will run only 'general'

@box.command('gif', channel=not_(Cs.do_not_use_gif))
async def gif():
   # this will not run at 'do_not_use_gif'

For using yui without change codes, You must set these channel keys like it.

[CHANNELS]
general = '_general'
game = 'game'
game_and_test = ['game', 'test']
welcome = '_general'
DATABASE_URL

string. URL to connect Database via SQLAlchemy.

DATABASE_ECHO

bool. If you set it to true, you can see raw SQL in log

OWNER_ID

string. ID of owner. You can get ID value from this test page

NAVER_CLIENT_ID

string. ID for using Naver API. If you want to use yui.apps.compute.translate or yui.apps.search.book, you must need this setting.(You can get this value from Naver developer page)

NAVER_CLIENT_SECRET

string. SECRET Key for using Naver API. Do not upload this value on VCS.

GOOGLE_API_TOKEN

string. API Token for using Google map API. You can generate this value on Google API Console and this activation page Do not upload this value on VCS.

AQI_API_TOKEN

string. API Token for using AQI API. You can get this value on this request form Do not upload this value on VCS.

TDCPROJECT_KEY

string. API Key for using SK Telecom Developer API. It is required by fetching holiday name and monday info. Do not upload this value on VCS.

WEBSOCKETDEBUGGERURL

string. URL of Chrome websocket debugger. This is using for access webpage via headless Chrome for bypass anti-DDoS tool such as CloudFlare.

WEBSOCKETDEBUGGERURL = 'http://localhost:9222/json/version'

You can launch headless chrome by this command.

docker run --rm --name headless-chrome -d -p 9222:9222 --cap-add=SYS_ADMIN yukinying/chrome-headless-browser
LOGGING

complex dict. Python logging config. You can use default setting. But if you want to change some value, you can override below example.

[LOGGING]
version = 1
disable_existing_loggers = false

[LOGGING.formatters.brief]
format = '%(message)s'

[LOGGING.formatters.default]
format = '%(asctime)s %(levelname)s %(name)s %(message)s'
datefmt = '%Y-%m-%d %H:%M:%S'

[LOGGING.handlers.console]
class = 'logging.StreamHandler'
formatter = 'brief'
level = 'DEBUG'
filters = []
stream = 'ext://sys.stdout'

[LOGGING.handlers.file]
class = 'logging.handlers.RotatingFileHandler'
formatter = 'default'
level = 'WARNING'
filename = 'log/warning.log'
maxBytes = 102400
backupCount = 3

[LOGGING.loggers.yui]
handlers = ['console', 'file']
propagate = true
level = 'DEBUG'

Run

$ yui run -c yui.config.toml

If you do not want to write -c option everytime, you can put it into envvar.

$ export YUI_CONFIG_FILE_PATH="yui.config.toml"
$ yui run

CLI for Database

Yui CLI support most of command of Alembic. You can use command with yui such as pipenv run yui revision --autogenerate -m "Test".

List of commands are below.

  • init-db
  • revision
  • migrate (same as revision with --autogenerate
  • edit
  • merge
  • upgrade
  • downgrade
  • show
  • history
  • heads
  • branches
  • current
  • stamp

Yui with Docker-compose

You can launch yui on docker-compose easily.

  1. Install Docker-compose.
  2. Craete docker-compose.yml file.

    version: '3'
    services:
      bot_item4:
        image: item4/yui:latest
        volumes:
          - .:/yui/data
        environment:
          - YUI_CONFIG_FILE_PATH=data/yui.config.toml
        depends_on:
          - db
        links:
          - db
        command: ./data/run.sh
      db:
        image: postgres:alpine
        volumes:
          - ./postgres/data:/var/lib/postgresql/data
        environment:
          - POSTGRES_PASSWORD=MYSECRET
        healthcheck:
          test: "pg_isready -h localhost -p 5432 -q -U postgres"
          interval: 3s
          timeout: 1s
          retries: 10
  3. Pull images

    $ docker pull item4/yui
    $ docker pull postgres:alpine
  4. Launch db container and create database

    $ docker-compose up -d db
    $ docker ps  # and see container name
    $ docker exec -it <CONTAINER_NAME_HERE> psql -U postgres  # and typing create database dbname; for create db
  5. Create config file with db info
  6. Launch Yui

    $ docker-compose up -d

You can see example files on example directory at this repo.

Contribute to YUI

YUI must keep PEP-8 and some rules. So you must run poetry install first and install pre-commit hook by below commands.

$ mkdir -p .git/hooks/
$ ln -s $(pwd)/hooks/pre-commit .git/hooks

License

Currently, YUI is under AGPLv3 or higher. But I have a plan to make slack bot framework from code of yui and switch it to MIT.

About

YUI, Slack Bot for item4.slack.com

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 99.2%
  • Other 0.8%