2015-05-26 21:18:50 -04:00
|
|
|
#
|
|
|
|
# Copyright 2015 (c) Anna Schumaker.
|
|
|
|
#
|
|
|
|
from . import api
|
|
|
|
from . import auth
|
2015-05-28 13:24:15 -04:00
|
|
|
from . import users
|
|
|
|
from datetime import datetime
|
|
|
|
import textwrap
|
|
|
|
|
|
|
|
|
|
|
|
class Message:
|
|
|
|
def __init__(self, json):
|
|
|
|
self.__ts = json["ts"]
|
|
|
|
self.__time = datetime.fromtimestamp(float(self.__ts))
|
|
|
|
self.__user = users.info(json["user"])
|
2015-05-28 13:51:01 -04:00
|
|
|
self.__text = [ json["text"].encode("utf-8").decode("latin-1") ]
|
2015-05-28 13:24:15 -04:00
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
return self.__time < other.__time
|
|
|
|
|
|
|
|
def __str__(self):
|
2015-05-28 13:51:01 -04:00
|
|
|
lines = []
|
|
|
|
for line in self.__text:
|
|
|
|
lines += [ "" ] + textwrap.wrap(line)
|
|
|
|
text = "\n ".join(lines)
|
2015-05-28 13:24:15 -04:00
|
|
|
time = self.__time.strftime("%I:%M:%S %p")
|
2015-05-28 13:51:01 -04:00
|
|
|
return "\n%s | %s:\n%s\n" % (time, self.__user, text)
|
2015-05-28 13:24:15 -04:00
|
|
|
|
|
|
|
def ts(self):
|
|
|
|
return self.__ts
|
|
|
|
|
2015-05-28 13:51:01 -04:00
|
|
|
def user(self):
|
|
|
|
return self.__user
|
|
|
|
|
|
|
|
def merge(self, msg):
|
|
|
|
self.__text += msg.__text
|
|
|
|
|
2015-05-28 13:24:15 -04:00
|
|
|
|
2015-05-26 21:18:50 -04:00
|
|
|
|
|
|
|
def postMessage(channel, text):
|
|
|
|
api.call("chat.postMessage", token = auth.token(), channel = channel,
|
2015-05-27 11:10:47 -04:00
|
|
|
text = text, as_user = True, parse = "full")
|