zulip/tools/iframe-bot/show-last-messages

157 lines
3.8 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright © 2013 Zulip, Inc.
import sys
sys.path.append('zulip')
from os import path
import optparse
import codecs
usage = """show-last-messages --user=<bot's email address> --api-key=<bot's api key> --count=<count>
Shows the last <count> messages on all public streams.
Examples: subscribe --user=tabbott@zulip.com --api-key=a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5 --count=5 --streams=social
subscribe --user=tabbott@zulip.com --api-key=a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5 --streams='foo bar'
You can omit --user and --api-key arguments if you have a properly set up ~/.zuliprc
"""
sys.path.append(path.join(path.dirname(__file__), '..'))
import zulip
parser = optparse.OptionParser(usage=usage)
parser.add_option_group(zulip.generate_option_group(parser))
parser.add_option('--streams', default='')
parser.add_option('--count', default=5)
(options, args) = parser.parse_args()
client = zulip.init_from_options(options)
if options.streams == "":
print >>sys.stderr, "Usage:", parser.usage
sys.exit(1)
client.add_subscriptions([{"name": stream_name} for stream_name in
options.streams.split()])
queue = client.register(event_types=['message'])
client._register('get_old_messages', method='GET', url='messages')
result = client.get_old_messages({'anchor': queue['max_message_id'],
'num_before': int(options.count),
'num_after': 0,
'apply_markdown': True})
if result['result'] != 'success':
sys.exit(1)
f = codecs.open("output-candidate.html", encoding='utf-8', mode="wb")
def uniwrite(f, s):
f.write(s)
uniwrite(f,"""
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="refresh" content="120" />
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700' rel='stylesheet' type='text/css'>
</head>
<body>
<script>
var t=setTimeout(function(){ window.scrollTo(0,document.body.scrollHeight); },500)
</script>
<style type="text/css">
body {
font-family: 'Source Sans Pro', 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 14px;
background-color: #eee;
}
p,ul,code,pre {
margin-top: 3px;
margin-bottom: 3px;
}
.message {
background-color: white;
border: 1px solid grey;
border-radius: 0.25em;
padding: 0.25em;
padding-left: 0.5em;
margin-bottom: 5px;
}
.avatar {
width: 25px;
height: 25px;
margin-right: 10px;
border-radius: 25px;
border: 1px solid #CCC;
float: right;
}
.sender {
font-weight: bold;
}
.recipient {
margin-right: 0.5em;
border-radius: 0.25em;
padding: 2px;
padding-left: 5px;
padding-right: 5px;
display: inline;
}
.twitter-tweet {
border: 1px solid #ddd;
padding: .5em .75em;
margin-bottom: 0.25em;
}
.twitter-avatar {
float: left;
width: 48px;
height: 48px;
margin-right: .75em;
}
</style>
<div>
<p>For more, join the conversation on <a href="https://zulip.com/register/customer3.invalid" target="_blank">Zulip</a>.</p>
""")
color = {'event1': '#76ce90', 'hongkong': '#fae589', 'etc': '#a6c7e5'}
for msg in result['messages']:
if msg['type'] != 'stream':
# Don't show PMs the bot got
continue
uniwrite(f,'<div class="message">')
uniwrite(f,'<img class="avatar" src="%s" />' % msg['avatar_url'])
uniwrite(f,'<div class="sender-bar">')
uniwrite(f,'<div class="recipient" style="background-color: %s;">%s &gt; %s</div>' % (color[msg['display_recipient']], msg['display_recipient'], msg['subject']))
uniwrite(f,'<span class="sender">%s</span>' % msg['sender_full_name'])
uniwrite(f,'</div>')
uniwrite(f,'<div class="content">%s</div>' % msg['content'])
uniwrite(f,"</div>")
uniwrite(f,"""
</div>
</body>
</html>
""")
sys.exit(0)