Skip to main content

Officially supported Keybase python bot client library

Project description

pykeybasebot

PyPI Build Status

This 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 for JavaScript and Go.

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), ...

Installation

pip install pykeybasebot

Python 3.7 or greater, please. And it's all async, so you'll need to call into it with that in mind.

Setup

Generally speaking, here's what you need to do:

  1. 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.
  2. Create a bot. You must initialize this with the handler function to call with each event. You may optionally 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 to keybase which 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.
  3. If you are not already running on a logged-in device, you need to do that. We recommend doing this with the oneshot command. It's in the examples.
  4. start the bot inside the asyncio event loop. This bot command wraps keybase chat api-listen, (and it takes basically the same exact options) and fires off events to your handler function.

Examples

Definitely definitely check out the examples. We're really counting on them to make it clear how to use this library.

Contributing

PRs are extremely welcome. To start:

git clone https://github.com/keybase/pykeybasebot
cd pykeybasebot

We use Poetry to handle our packaging. Go check out their website for installation instructions. To start Poetry, you'll need the python executable in your path to link to Python 3.7. We recommend using pyenv to handle different versions of Python on your machine. With pyenv installed, it should automatically set python to 3.7 when you cd into this repo.

Once you have the right Python version, you can run:

pip install poetry
poetry install

This 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}.whl

Static code analysis tools

We use a few different static analysis tools to perform linting, type-checking, formatting, etc. The correct versions should be install when you run poetry install, but you'll probably want to configure your editor to work with:

pre-commit hooks

We check all git commits with the above tools with pre-commit.com pre-commit hooks. To enable use of these pre-commit hooks:

  • Install the pre-commit utility.
  • Remove any existing pre-commit hooks via rm .git/hooks/pre-commit
  • Configure via pre-commit install

Then proceed as normal.

Testing

To run tests, type

make test

Tests are admittedly weak. You could change that!

Types

Most of the types the bot uses are generated from definitions defined in the protocol/ 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 the client repo. This requires Go and your GOPATH to be set up.

go get github.com/keybase/client/go/keybase

and install the necessary dependencies for compiling the protocol files. This requires node.js and Yarn.

cd client/protocol
yarn install

Then you can generate the types by using the provided Makefile in this repo.

cd path/to/keybase-bot
make types

Should you need to remove all the types for some reason, you can run make clean.

Publishing

Poetry 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.

poetry build
# Upload to Test PyPi. You only need to run the first command once
poetry config repositories.testpypi https://test.pypi.org/legacy/
poetry publish -r testpypi
# Upload to real PyPi
poetry publish

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pykeybasebot-0.2.1.tar.gz (79.3 kB view details)

Uploaded Source

Built Distribution

pykeybasebot-0.2.1-py3-none-any.whl (79.8 kB view details)

Uploaded Python 3

File details

Details for the file pykeybasebot-0.2.1.tar.gz.

File metadata

  • Download URL: pykeybasebot-0.2.1.tar.gz
  • Upload date:
  • Size: 79.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.3 Darwin/18.7.0

File hashes

Hashes for pykeybasebot-0.2.1.tar.gz
Algorithm Hash digest
SHA256 7581aed4afd80923567820e8a219489b67b144d7ffadd3faee2fefc3d2914acb
MD5 f9a78f5061131b4377323408f034a445
BLAKE2b-256 5ff2b6cc95156826cc2e15a168d2f8e5786c2536530ee3e04b57adea3a7b02ac

See more details on using hashes here.

File details

Details for the file pykeybasebot-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: pykeybasebot-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 79.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.3 Darwin/18.7.0

File hashes

Hashes for pykeybasebot-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b84414c6dad3d69bbaf77abc59a9db83916af0718072e7b92af4104dec862ea5
MD5 b7c050a52923129143a360d0c252c890
BLAKE2b-256 fa9e5a1448d8cee40a980e6affc23b26102fca68f99b460a4d9a3c49935bc28d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page