Package: pykeybasebot
pykeybasebotThis is the officially support Keybase Python library. It is an unopinionated, simple wrapper around the Keybase CLI API for creating an interactive bot or general scripting. This library does not attempt to do intent parsing or manage state at all. You'll have to build that yourself, but with the examples, this library will hopefully make whatever you want to build much much easier:).There are also similar libraries forJavaScriptandGo.This library is very far from exhaustively covering the complete Keybase API, but it is our hope that it will be easy to add to (see chat_client.py for the pattern). It currently does reading from channels and writing messages/reactions pretty well. That's enough for the vast majority of basic functionality. Future work can add teams behavior, more wallet functionality (e.g. sending money), ...Installationpip install pykeybasebotPython 3.7 or greater, please. And it's all async, so you'll need to call into it with that in mind.SetupGenerally speaking, here's what you need to do:Create a handler function that takes event objects and does something with them. This function will get called with your bot instance (described below) and the KbEvent instance.Create a bot. Youmustinitialize this with the handler function to call with each event. Youmayoptionally pass in: (1) the username and/or paperkey for the bot's identity (it'll default to the currently logged-in user otherwise), (1) the event loop that you want new tasks to be sent to (this is necessary if you want to lock on async behavior -- see the examples), (2) the location of the running keybase app (defaults tokeybasewhich is fine if it's in your PATH), your user's home directory, or pid_file. These three are more useful for complicated local development with multiple accounts and less useful if you're running in a docker container or as the only user on your system.If you are not already running on a logged-in device, you need to do that. We recommend doing this with theoneshotcommand. It's in the examples.start the bot inside the asyncio event loop. This bot command wrapskeybase chat api-listen, (and it takes basically the same exact options) and fires off events to your handler function.ExamplesDefinitely definitely check out the examples. We're really counting on them to make it clear how to use this library.ContributingPRs are extremely welcome. To start:git clone https://github.com/keybase/pykeybasebot
cd pykeybasebotWe usePoetryto handle our packaging. Go check out their website for installation instructions. To start Poetry, you'll need thepythonexecutable in your path to link to Python 3.7. We recommend usingpyenvto handle different versions of Python on your machine. With pyenv installed, it should automatically setpythonto 3.7 when youcdinto this repo.Once you have the right Python version, you can run:pip install poetry
poetry installThis will set up a virtualenv for you and install all the dependencies needed into it!Remember that if you're making changes to pykeybasebot and want to test them
locally, you'll need to first uninstall previously installed pykeybasebot,
then install your local version:pip uninstall pykeybasebot
poetry build
pip install ./dist/pykeybasebot-{tags}.whlStatic code analysis toolsWe use a few different static analysis tools to perform linting, type-checking, formatting, etc. The correct versions should be install when you runpoetry install, but you'll probably want to configure your editor to work with:mypy(Type checking)black(code formatting)isort(import formatting)flake8(linting)pre-commit hooksWe check all git commits with the above tools withpre-commit.compre-commit hooks.
To enable use of these pre-commit hooks:Installthepre-commitutility.Remove any existing pre-commit hooks viarm .git/hooks/pre-commitConfigure viapre-commit installThen proceed as normal.TestingTo run tests, typemake testTests are admittedly weak. You could change that!TypesMost of the types the bot uses are generated from definitions defined in theprotocol/directory inside the Keybase client repo. This ensures that the types that the bot uses are consistent across bots and always up to date with the output of the API.To build the types for the Python bot, you'll need to clone theclientrepo. This requiresGoand yourGOPATHto be set up.gogetgithub.com/keybase/client/go/keybaseand install the necessary dependencies for compiling the protocol files. This requiresnode.jsandYarn.cdclient/protocol
yarninstallThen you can generate the types by using the provided Makefile in this repo.cdpath/to/keybase-bot
maketypesShould you need to remove all the types for some reason, you can runmake clean.PublishingPoetry can build and publish packages to PyPI. We've run into some issues with uploading to PyPI and Poetry, though, so for now we're recommending building with Poetry and uploading with Twine.poetrybuild# Upload to Test PyPi. You only need to run the first command oncepoetryconfigrepositories.testpypihttps://test.pypi.org/legacy/
poetrypublish-rtestpypi# Upload to real PyPipoetrypublish