This repository has been archived by the owner on Jun 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Documentation init & slack app quickstart
- Loading branch information
Showing
13 changed files
with
287 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
python: | ||
pip_install: true | ||
extra_requirements: | ||
- doc | ||
conda: | ||
file: docs/environment.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
============= | ||
API Reference | ||
============= | ||
|
||
Slack Plugin | ||
------------ | ||
|
||
.. module:: sirbot.plugins.slack | ||
|
||
.. autoclass:: sirbot.plugins.slack.SlackPlugin | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[Dolphin] | ||
PreviewsShown=true | ||
Timestamp=2018,3,1,14,51,48 | ||
Version=4 |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
name: sir-bot-a-lot-doc | ||
dependencies: | ||
- python=3.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
.. _howto: | ||
|
||
====== | ||
How To | ||
====== | ||
|
||
.. toctree:: | ||
:glob: | ||
:maxdepth: 2 | ||
|
||
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
================= | ||
Slack Application | ||
================= | ||
|
||
Quickstart for creating a slack application with Sir bot-a-lot. | ||
|
||
Ngrok setup | ||
----------- | ||
|
||
For ease of coding we will develop our app locally but in order to do so we need a tool to be reachable by slack. For | ||
this we will use `ngrok <https://ngrok.com/>`_. | ||
|
||
* `Install <https://ngrok.com/download>`_ ngrok. | ||
* Start an HTTP tunnel on port ``8080``. | ||
|
||
.. code:: console | ||
$ ngrok http 8080 | ||
You should now have access to the ngrok web interface at `127.0.0.1:4040 <http://127.0.0.1:4040>`_ and see your http & https forwarding url (``http://xxxxxx.ngrok.io -> localhost:8080``). | ||
|
||
|
||
Application setup | ||
----------------- | ||
|
||
* `Create a development slack team <https://slack.com/get-started#create>`_. | ||
* `Create a new app <https://api.slack.com/apps>`_. | ||
* Add the ``chat:write:bot`` & ``channels:read`` permissions to your application from the ``OAuth & Permissions`` page. | ||
|
||
.. image:: ../assets/slack_app_permissions.png | ||
:align: center | ||
|
||
* Install the application to your workspace (Preferably on all public channels). | ||
* Grab the ``Verification Token`` from the ``Basic Information`` page. | ||
|
||
.. image:: ../assets/slack_verification_token.png | ||
:align: center | ||
|
||
* Grab the ``OAuth Access Token`` from the ``Install App`` page. | ||
|
||
.. image:: ../assets/slack_oauth_token.png | ||
:align: center | ||
|
||
Sir Bot-a-lot installation | ||
-------------------------- | ||
|
||
`Sir Bot-a-lot is on PyPI <https://pypi.org/project/sirbot/>`_. | ||
|
||
.. code:: console | ||
$ python3 -m venv .env # Create a virtual environment | ||
$ source .env/bin/activate # Activate the virtual environment | ||
$ pip3 install sirbot # Install Sir bot-a-lot | ||
Starting the Bot | ||
---------------- | ||
|
||
* Copy the code in a ``bot.py`` file and fill the ``OAuth Access Token`` & ``Verification Token``. | ||
|
||
.. code:: python3 | ||
import logging | ||
logging.basicConfig(level=logging.DEBUG) | ||
from sirbot import SirBot | ||
from sirbot.plugins.slack import SlackPlugin | ||
OAUTH_ACCESS_TOKEN = '' | ||
VERIFICATION_TOKEN = '' | ||
bot = SirBot() | ||
slack = SlackPlugin(token=OAUTH_ACCESS_TOKEN, verify=VERIFICATION_TOKEN) | ||
bot.load(slack) | ||
bot.start(host='0.0.0.0', port=8080) | ||
* Start your bot | ||
|
||
.. code:: console | ||
$ (.env) python bot.py | ||
Now that your bot is running we can start sending it events. | ||
|
||
* Go back on your application configuration page (click `here <https://api.slack.com/apps>`_ to find it back). | ||
* Add the ``Event Subscriptions`` feature. Set the url as your ngrok url (``http://xxxxxx.ngrok.io``). | ||
* If everything is ok the url should be marked as ``Verified``. | ||
* Add the ``message.channels`` event to the subscription. | ||
|
||
.. image:: ../assets/slack_event_subscriptions.png | ||
:align: center | ||
|
||
* Type a message in your slack channel and you should see it coming in the logs. | ||
|
||
.. code:: | ||
sirbot.plugins.slack.endpoints - DEBUG - Incoming message: Slack Message: {'type': 'message', 'user': 'U29163YQH', 'text': 'test message', 'ts': '1519907093.000242', 'channel': 'C5MKJ21QR', 'event_ts': '1519907093.000242'} | ||
Answering messages | ||
------------------ | ||
|
||
Once we receive message the next step is to start answering them. For that we need to add some code to ``bot.py``. | ||
|
||
.. code:: python3 | ||
... | ||
from slack import methods # Enum of the slack endpoints for better autocomplete | ||
... | ||
# We create function that will be called on incoming message | ||
# The parameters are the incoming message and the aiohttp app | ||
async def hello(message, app): | ||
# We create a response object from the incoming message | ||
response = message.response() | ||
# We set the text of our message | ||
response['text'] = 'Hello <@{user}>'.format(user=message['user']) | ||
# We querry the slack api chat.postMessage with our response | ||
await app['plugins']['slack'].api.query(url=methods.CHAT_POST_MESSAGE, data=response) | ||
... | ||
slack = SlackPlugin(token=OAUTH_ACCESS_TOKEN, verify=VERIFICATION_TOKEN) | ||
# We register our function to respond to message starting with ``hello`` | ||
slack.on_message('^hello', hello) | ||
bot.load(slack) | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,61 @@ | ||
=============== | ||
Sir-bot-a-lot-2 | ||
=============== | ||
========================================== | ||
Sir Bot-a-lot | Asynchronous bot framework | ||
========================================== | ||
|
||
This project aims to provide a simple library to generate bots for various services. The initial goal was to implement | ||
a bot for the slack team of the `pyslackers <https://www.pyslackers.com>`_ community. | ||
|
||
Installation | ||
------------ | ||
|
||
`Sir Bot-a-lot is on PyPI <https://pypi.org/project/sirbot/>`_. | ||
|
||
.. code:: console | ||
$ pip3 install sirbot | ||
We recommend installing it in a `virtual environment <https://docs.python.org/3/tutorial/venv.html>`_. | ||
|
||
To run the bot initialize it and call the ``bot.start`` method: | ||
|
||
.. code:: python3 | ||
from sirbot import SirBot | ||
bot = SirBot() | ||
bot.start(host='0.0.0.0', port=80) | ||
For examples take a look at the :ref:`howto` and the `pyslackers bot <https://github.com/pyslackers/sirbot-pyslackers>`_. | ||
|
||
Plugins | ||
------- | ||
|
||
Sir Bot-a-lot connect to services with plugins, some are bundled by default: | ||
|
||
* ``sirbot.plugins.github.GithubPlugin`` For `Github <https://www.github.com>`_. | ||
* :class:`sirbot.plugins.slack.SlackPlugin` For `Slack <https://www.slack.com>`_. | ||
* ``sirbot.plugins.postgres.PgPlugin`` For `PostgreSQL <https://www.postgresql.org/>`_. | ||
* ``sirbot.plugins.apscheduler.APSchedulerPlugin`` For `APscheduler <https://apscheduler.readthedocs.io/en/latest/>`_. | ||
|
||
To load a plugin call the ``bot.load`` method: | ||
|
||
.. code:: python3 | ||
from sirbot import SirBot | ||
from sirbot.plugins.slack import SlackPlugin | ||
bot = SirBot() | ||
slack = SlackPlugin(token='supersecureslacktoken') | ||
bot.load(slack) | ||
bot.start(host='0.0.0.0', port=80) | ||
Navigation | ||
---------- | ||
|
||
.. toctree:: | ||
:maxdepth: 2 | ||
|
||
how_to/index | ||
api/index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters