Skip to content

Instantly share code, notes, and snippets.

@imsickofmaps
Created April 20, 2013 12:18
Show Gist options
  • Save imsickofmaps/5425810 to your computer and use it in GitHub Desktop.
Save imsickofmaps/5425810 to your computer and use it in GitHub Desktop.
New and improved version of smitter - twitter-clone over SMS. Build on top of ScaleConf workshop instructions http://vumi.readthedocs.org/en/latest/intro/scaleconf03.html
from twisted.internet.defer import inlineCallbacks, returnValue
from vumi.application import ApplicationWorker
from vumi.persist.txredis_manager import TxRedisManager
from vumi.config import ConfigDict
class SmitterApplicationConfig(ApplicationWorker.CONFIG_CLASS):
redis_manager = ConfigDict(
"Redis client configuration.", default={}, static=True)
class SmitterApplication(ApplicationWorker):
CONFIG_CLASS = SmitterApplicationConfig
@inlineCallbacks
def setup_application(self):
config = self.get_static_config()
# This is the key in redis where we will store our set of
# users across restarts
self.members_key = 'SmitterMembers'
# load and expose redis to the application
self.redis = yield TxRedisManager.from_config(
config.redis_manager)
@inlineCallbacks
def broadcast(self, content, sender):
"""
Sends a broadcast to everyone except the person sending it.
Returns the number of messages sent so the person can be notified
"""
members = yield self.redis.smembers(self.members_key)
sent = 0
for member in members:
if member != sender:
yield self.send_to(member, content)
sent += 1
returnValue(sent)
@inlineCallbacks
def consume_user_message(self, message):
"""
The main switch for consuming smitter messages.
Any message that is starting with one of the codes below is
just broadcast. Initial codes for intelligence:
+ : join channel
- : leave channe1
? <number> : invite friend (has to reply + to join)
@ : asks people where they are
# : sets topic that the person wants to discuss
"""
if message['content'] == '+':
# Join channel
yield self.redis.sadd(self.members_key, message.user())
yield self.reply_to(
message, 'You have joined Smitter! SMS "-" to leave, \
"? <num>" to invite, "@" to ask locations, \
"# <topic>" to broadcast subject')
elif message['content'] == '-':
# leave channel
yield self.redis.srem(self.members_key, message.user())
yield self.reply_to(
message, 'You have left Smitter. SMS "+" to join.')
elif message['content'].startswith('?'):
# invite <number>
yield self.reply_to(
message, 'Invited your buddy')
yield self.send_to(message['content'].split()[1],
'Join this group by replying \'+\'')
elif message['content'].startswith('@'):
# request location
yield self.reply_to(
message, 'You\'ve asked where everyone is.')
yield self.broadcast('Someone wants to know where everyone is. \
Reply with your location.', message.user())
elif message['content'].startswith('#'):
# set topic
topic = message['content'][2:]
yield self.reply_to(
message, 'You changed the topic to ' + topic)
yield self.broadcast('Someone changed the topic to: '
+ topic, message.user())
else:
# broadcast and get total sent
sent = yield self.broadcast(message['content'], message.user())
yield self.reply_to(
message, 'Broadcast to %s members' % sent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment