Created
February 4, 2019 16:17
-
-
Save aeppert/43de859c631a907ad68ed3401cb0bf42 to your computer and use it in GitHub Desktop.
Slack messages via Python
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
import os | |
import socket | |
import slack | |
import slack.chat | |
from optparse import OptionParser, OptionGroup | |
G_USERNAME = 'MY_USERNAME' | |
G_CHANNEL = '#MY_CHANNEL' | |
G_API_TOKEN = 'MY_API_TOKEN' | |
class slackmsg: | |
def __init__(self, key, username=G_USERNAME, channel=G_CHANNEL): | |
self.key = key | |
self.username = username if username else G_USERNAME | |
self.channel = channel if channel else G_CHANNEL | |
slack.api_token = self.key | |
def post(self, message): | |
slack.chat.post_message(self.channel, message, username=self.username) | |
def main(): | |
parser = OptionParser() | |
parser.add_option('--chan', dest='channel', help='Channel name') | |
parser.add_option('--user', dest='username', help='Post message using username') | |
parser.add_option('--msg', dest='msg', help='Message to post to channel') | |
(options, args) = parser.parse_args() | |
if len(sys.argv) == 1 or options.msg == None: | |
parser.print_help() | |
sys.exit(1) | |
pss = slackmsg(G_API_TOKEN, options.username) | |
pss.post(t_msg) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment