Giter VIP home page Giter VIP logo

tangerine's Introduction

Tangerine logo

pypi

A Flask inspired, decorator based API wrapper for Python-Slack.

About

Tangerine is a lightweight Slackbot framework that abstracts away all the boilerplate code required to write bots, allowing you to focus on the problem at hand.

Installation

  1. To install tangerine, simply use pipenv (or pip, of course):
$ pipenv install slack-tangerine
  1. Create a new file with the following contents:
# mybot.py
from tangerine import Tangerine
tangerine = Tangerine("xoxb-1234567890-replace-this-with-token-from-slack")


@tangerine.listen_for('morning')
def morning(user, message):
    return "mornin' @{user.username}"

if __name__ == '__main__':
   tangerine.run()
  1. Now try running it, run the following command then say "morning" in Slack.
python mybot.py

Usage

To start your project, you'll first need to import tangerine by adding from tangerine import Tangerine to the top of your file.

Next you'll need to create an instance of Tangerine and configure your Slack token. This can be done using a yaml config file or passing it explicitly to the initialization.

# Option 1: YAML config:
import os
from tangerine import Tangerine

path = os.path.dirname(os.path.abspath(__file__))
path_to_yaml = os.path.join(path, 'config.yaml')
tangerine = Tangerine.config_from_yaml(path_to_yaml)

# Option 2: Hardcoded slack token
from tangerine import Tangerine
tangerine = Tangerine("xoxb-1234567890-replace-this-with-token-from-slack")

Now its time to write your response functions, these functions get wrapped with the listen_for decorator, which registers a pattern to watch the slack conversation for and which python method should handle it once its said.

In the following example, the method is setup to listen for the word "cookies". Notice that the decorator passes two arguments to the function, first the user object which contains information about the user who triggered the event (in this case the Slack user who said the word cookies) and message, which is a string of the complete message.

@tangerine.listen_for('cookies')
def cookies(user, message):
    # do something when someone say's "cookies" here.

Crontab

Sometimes you'll run into situations where you want Slack messages to be sent periodically rather than in direct response to a keyword, for this Tangerine ships with a single-threaded Python implementation of Cron.

Let's pretend we want to send a message to everyone in a channel every five minutes, simply add the following to your mybot.py file:

@tangerine.cron('*/5 * * * *')
def some_task():
    tangerine.speak("Hay Ride!", "#general")

tangerine's People

Contributors

dependabot-preview[bot] avatar gmeluski avatar johnbenjaminlewis avatar meganehouser avatar nficano avatar popsikle avatar svisser avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tangerine's Issues

As a Gendo, I should be sentient.

As a user, I expect Gendo to become self-aware and make a moral choice as to whether he is destined to help mankind or terminate mankind

Python 2.7 not able to load tangerine

Tested the bot on both python 2.7 and 3.7.0
The bot works perfectly on 3.7 but throws this error on 2.7

File “slackbot.py”, line 3, in
from tangerine import Tangerine
ImportError: No module named tangerine

Issues setting up environment

Hi Nick!
This looks like an awesome library and I am so excited to use it to update our current slackbot.

I've followed instructions as suggested and am running into environmental/dependency issues getting the basic bot script to work. This is the error I'm getting:

Traceback (most recent call last):
  File "<REDACTED>/bot.py", line 2, in <module>
    from tangerine import Tangerine
  File "<VENV_REDACTED>/lib/python3.7/site-packages/tangerine/__init__.py", line 10, in <module>
    from .bot import Tangerine
  File <VENV_REDACTED>/lib/python3.7/site-packages/tangerine/bot.py", line 17, in <module>
    from slackclient import SlackClient
ModuleNotFoundError: No module named 'slackclient'

And here is my Pipfile:

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
slack-tangerine = "*"
crontab = "*"
jinja2 = "*"
python-box = "*"
pyyaml = "*"
six = "*"
slackclient = "2.9.3"

[requires]
python_version = "3.7"

And it shows the Pipfile.lock reflects the slackclient.

        },
        "slack-tangerine": {
            "hashes": [
                "sha256:92fa45aa2c2c75594212375a82a52e1ff8edc94b5dcb18b9d626efcc14367fce",
                "sha256:b513504e26d21d6d0421d7edf0eb8417f6b57e6fb078b6cec0d0ca80558eb8a8"
            ],
            "index": "pypi",
            "version": "==5.1.0"
        },
        "slackclient": {
            "hashes": [
                "sha256:07ec8fa76f6aa64852210ae235ff9e637ba78124e06c0b07a7eeea4abb955965",
                "sha256:2d68d668c02f4038299897e5c4723ab85dd40a3548354924b24f333a435856f8"
            ],
            "index": "pypi",
            "version": "==2.9.3"
        },

Tangerine returns messages several times

Hi, I'm trying to detect a message with a specific keyword, and tried this example

`from tangerine import Tangerine
tangerine = Tangerine("xoxb-xx")
@tangerine.listen_for('m')
def getspa(user, message):
s = 'I see you like ' + message
print(message)
return s

@tangerine.listen_for('morning')
def morning(user, message):
return "mornin' @{user.username}"

if name == "main":
tangerine.run()
`
The problem is that, I received several repeated messages, so for example , I entered:
just the letter m I got:
I see you like m
I see you like I see you like m
I see you like I see you like I see you like m
and it goes on and on till I kill it.
am I doing something wrong?
Thanks

Is it possible to specified a channel for the bot to handle?

Is it possible to specified a channel for the slack-bot to handle?

Example with "#general" channel:

Method1

@tangerine.listen_for("hello")
def hello(user, message, channel):
if channel == "#general":
return "world"

Or

Method2

@tangerine.listen_for("hello", "#general")
def hello(user, message):
return "world"

I prefer combine both Method1 and Method2. Just like:

Method3

@tangerine.listen_for("hello", ["#general", "chatroom", "faq"])
def hello(user, message, channel):
if channel == "#general":
return "world"
elif channel == "#chatroom":
return "hello"
elif channel == "#faq":
return "WORLD"

As a drunk user, gendo should filter my requests

Use Case 1:
Request includes "image" and any term from our predetermined phallic dictionary
i.e. Drunk user enters "@gendo image me cat dicks", gendo responds "Go home you're drunk"

Use Case 2:
Request includes a personal attack on your friends that slightly crosses the line
i.e. username is "alleycat" and request includes at least two typos

Help with crontab

@gendo.cron('0 9 * * *') def cookies(user, message): return 'morning everyonee'

can you help me why the cron for this not working. how to fix it

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.