Giter Site home page Giter Site logo

bagel's Introduction

Slack Scheduler

A Python 3.6 script that sends a message to a Slack Channel using Incoming WebHooks. The script uses APScheduler to schedule the message and requests to post the message to the Slack Channel.

Slack Incoming WebHooks

The script assumes you have created a custom integration for Incoming WebHooks for your Slack Group and have an Incoming Webhook URL where you can send JSON payloads.

If not, you will need to log into your Slack Group and create a custom Incoming WebHook for use with this script.

Environment Variables

The script reads 1 environment variable, called SLACK_INCOMING_URL, that should contain the URL of the Incoming WebHook.

SLACK_INCOMING_URL
export SLACK_INCOMING_URL="{your URL}"

Getting Started

The best way to run the script is to clone the github repository. Once you clone the repository, it is best to create a virtual environment and pip install the requirements.

Step 1: Clone the Repository

$ git clone https://github.com/davidhayden/slack-scheduler.git

Step 2: Create Virtual Environment and Install Requirements

Change into the project directory, create a virtual environment, and pip install the requirements in the requirements.txt file.

$ cd slack-scheduler

$ python -m venv env
$ source env/bin/activate
$ (env) pip install -r requirements.txt

Step 3: Set Environment Variable

You must set 1 environment variable containing the Incoming Webhook URL of your Slack Group. It is convenient to set this variable in ~/.bash_profile so it gets set when you open a terminal window. However, you can set it at the bash prompt for testing.

$ (env) export SLACK_INCOMING_URL={your URL}

Step 4: Specify Slack Channel and Message Text

Specify the channel and text for the message in the send_message function. You can exclude the channel if you wish to accept the default channel you specified when creating the Incoming WebHook.

message = {
    "channel": "#general",
    "text": "A message."
}

Step 5: Specify the Message Interval

APScheduler is quite flexible. Currently the script sends the message to the Slack Channel every minute. You will want to modify this according to your application needs.

@sched.scheduled_job('interval', minutes=1)

Read APScheduler's documentation for more information on scheduling jobs.

http://apscheduler.readthedocs.io/en/latest/

Step 6: Run the Script

Run the script from the command line. You can press CTRL+C anytime to interrupt the script and return to the command line.

$ (env) python slack.py

Running...
{'date': '2017-12-14 16:55:29.362473', 'message': {'channel': '#general', 'text': 'A message.'}, 'statusCode': 200}
{'date': '2017-12-14 16:56:29.100282', 'message': {'channel': '#general', 'text': 'A message.'}, 'statusCode': 200}
{'date': '2017-12-14 16:57:29.270187', 'message': {'channel': '#general', 'text': 'A message.'}, 'statusCode': 200}

bagel's People

Contributors

haithemsouala avatar

Watchers

James Cloos avatar  avatar

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.