slackpost.py: Write a script for posting to slack

The idea is that postfix will pipe an email message into this script,
which will then post the reply to slack.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-05-26 21:18:50 -04:00
parent d9a6858be8
commit 6110e5ff3b
3 changed files with 44 additions and 0 deletions

View File

@ -3,6 +3,7 @@
#
from . import api
from . import auth
from . import chat
from . import users
from datetime import datetime
@ -35,6 +36,9 @@ class Channel:
def name(self):
return self.__name
def post(self, text):
chat.postMessage(self.__id, text)
def unread_count(self):
if self.__unread == None:
self.fetch_info()

11
slack/chat.py Normal file
View File

@ -0,0 +1,11 @@
#
# Copyright 2015 (c) Anna Schumaker.
#
from . import api
from . import auth
from urllib import request
def postMessage(channel, text):
post = request.quote(text)
api.call("chat.postMessage", token = auth.token(), channel = channel,
text = post, as_user = True, parse = "full")

29
slackpost.py Executable file
View File

@ -0,0 +1,29 @@
#!/usr/bin/python
# Copyright 2015 (c) Anna Schumaker.
#
import slack
import sys
from email.parser import Parser
headers = Parser().parsestr(sys.stdin.read())
if headers['from'] != "Anna Schumaker <schumaker.anna@gmail.com>":
sys.exit(1)
# Determine which channel to post to
channel = None
subject = headers['subject']
ch_name = subject.split("[%s]" % slack.team())[-1].strip()
for c in slack.channels.list():
if c.name() == ch_name:
channel = c
break
if channel == None:
sys.exit(1)
text = headers.get_payload().split("\n")[0]
channel.post(text)