Tekinbot
Tekinbot is a simple and expandable slackbot who runs on Python, uWSGI, love, and Slack's Event API, and who could serve great utility to your Slack workspace!
Preparation:
-
You need python3.6, together with MySQL and SQLite
-
First of all, set up a python virtual environment by running
make venv
-
Run
make install-hooks
to install pre-commit hooks -
git checkout -b <your-branch-name>
, and hack away! For this repo, I recommend using a meaningful branch name, and recommend using the formatgit checkout -b <github_username-issue_number-branch_name>
-
Run
make dev
to make a local server instance so you couldcurl
and test your changes, note that themake dev
instance runs with--dry-run
and--no-db
options, meaning that:- it does not send response requests, but instead prints all responses to stdout
- it does not write to mysql database, but uses an in-memory sqlite instance instead
- it is served with python builtin
wsgiref
library, instead ofuWSGI
you could also run (in venv)
python3 -m tekinbot.tekin
with any options, run with-h
or--help
to see the available dev options. -
After making your changes,
git push origin <your-branch-name>
, for this repo, let me know that your branch is ready to be merged -
(Optional) If you have a Slack workspace to test it on:
-
get your tekin's slack OAuth tokens and put them in a
tekin-secrets
config; by default, this config is in~/.tekin-secrets.yaml
, you need to have a section in that config likeslack: app_auth: xoxp-long-strings... bot_auth: xoxb-long-strings... tekin_id: <@UTEKIN123>
the
tekin_id
is the user id string of Tekinbot, it is used for interpreting@TekinBot
. You can get this string by adding Tekinbot in your workspace, @TekinBot, then copying the link of @TekinBot. It should look likehttps://<your-workspace>.slack.com/team/UTEKIN123
, the last section is thetekin_id
. We will try to automate this process by querying Slack API in a future branch.You will also need some database configs in this secrets file, see the Database section below.
For an example of this
tekin-secrets
config, see here. -
run
make server
and set your event subscription request URL in Slack tohttp://<your-tekin-url>:9338
, -
if you want another port, or some other uWSGI server related options, change the uwsgi config at /config/tekin_uwsgi_conf.yaml
-
You can then see your tekinbot addition in live action! EHHHHHHH!
-
-
You've dunnit! Remember what Tekin would say: take a 5 minute break, drink some water and wash your face...
How to add a new module:
-
All current modules can be seen in this directory, if you would like some examples to look at.
-
All the modules should have two methods:
process(request)
andpost(request, response)
. -
Some modules may need extra components; for example, all commands in
message
submodule need a component calledcomm_re
. Refer to the individual modules' README to see how they work. -
The
process()
method handles an incoming message, and decides how to respond to it. Therequest
parameter is the full http request generated by Slack. A sample of what it looks like could be seen here -
The
post()
method handles how a response should be posted, and any actions that are to be performed after the posting of the said messages. The utility modules have some functions for plain text posting, so usually you could just use these utilities if you are not trying to post special stuff, or to perform some pre/post-posting actions. -
Add your module to the comms/__init__.py, and its individual submodule's __init__.py
-
After implementing writing both of these functions and any other additional requirements that a module might have, you should then write unit tests for your comms module.
-
Commit your changes, fix any things that may be caught by the pre-commit hooks, then
make test
; if any of the test fails, fix accordingly. -
Manually Test your module by
make dev
thencurl
ing your local tekin instance with something similar to the example here.
How to Tekin database:
-
First of all, you will need
mysql-server
running on your dev if you want to do any manual testing at all. See here to see how to set it up on Ubuntu environments. This project uses SqlAlchemy ORM to deal with all the database transactions; for a quick tutorial see hereYou will also need to
CREATE
a database namedtekinbot
in MySQL.If you do not want to give Tekinbot
root
access to mysql-server, make a user and configure it intekin-secrets
config (see above), and make sure toGRANT
proper priviledges to the user you created; minimally, CRUD-related priviledges should be given. -
Figure out what kind of tables you will need, and make a table model similar to this one in the same directory (tekinbot/db/models); after you are done writing your table schemas, add your table to the db module
-
Implement your module; tekin uses a singleton as SqlAlchemy
engine
, and with that engine creates a singleSessionMaker
, to get these instances, use the utilities fromtekinbot.utils.db
-
To test on your machine, supply your MySQL-server's access credentials in the
.tekin-secrets.yaml
file, by default it is located at~/.tekin-secrets.yaml
, you can change the yaml file to be at a different location, but don't commit it as part of your change; make sure the yaml file a dictionary similar to this:database: username: <your-tekin-user-name> password: hunter2
-
When you
make dev
, all the tables in the directory tekinbot/db/models will be initiated if they don't exist yet. Run python, import your module, and test away! Though, do note thatmake dev
defaults to use an in-memory sqlite database; to use actual MySQL database, runpython3 -m tekinbot.tekin
without the--no-db
option. -
For NS's instance of Tekin, if after a feature has been shipped, you need a table model changed, figure out the correct
ALTER TABLE
command, and include it in your pull request.