class CampfireBot(object): """This is where all the actual message handling takes place. """ def __init__(self, subdomain, room_name, token, ssl=False): self.campfire = Campfire(subdomain, token, ssl) self.room = self.campfire.find_room_by_name(room_name) self.me = self.campfire.me() self.auth_token = token self.xmpp = None if not self.room: raise RuntimeError("Could not find room %s" % room) self.users = {} self.joined = False def connectionMade(self): print "connectionMade" def connectionFailed(self, why): print "connectionFailed" def get_user(self, user_id): if user_id in self.users: return self.users[user_id] else: u = self.campfire.user(user_id) self.users[user_id] = u return u def formatMessage(self, msg): fwd_msg = None user = ('user_id' in msg and msg['user_id'] is not None) and self.get_user(msg['user_id']) or None if msg['type'] == 'TextMessage': fwd_msg = "%s: %s" % (user['user']['name'], msg['body']) elif msg['type'] == 'LeaveMessage' or msg['type'] == 'KickMessage': fwd_msg = "- %s has left the room" % user['user']['name'] elif msg['type'] == 'EnterMessage': fwd_msg = "- %s has entered the room" % user['user']['name'] elif msg['type'] == 'PasteMessage': fwd_msg = "- %s pasted:\n%s" % (user['user']['name'], msg['body']) elif msg['type'] == 'UploadMessage': fwd_msg = "- %s uploaded %s" % (user['user']['name'], msg['body']) elif msg['type'] == 'TimestampMessage': fwd_msg = "- timestamp %s" % (msg['created_at']) return fwd_msg def messageReceived(self, msg): print unicode(msg) if 'user_id' in msg and msg['user_id'] == self.me['id']: # skip my own messages if msg['type'] in ('TextMessage', 'PasteMessage'): return if msg['type'] in ('TextMessage', 'LeaveMessage', 'KickMessage', 'EnterMessage', 'PasteMessage', 'UploadMessage'): fwd_msg = self.formatMessage(msg) if fwd_msg: if self.xmpp: self.xmpp.forwardMessage(fwd_msg) else: print fwd_msg def _registerProtocol(self, protocol): self._streamProtocol = protocol def disconnect(self): print "disconnect" if hasattr(self, "_streamProtocol"): self._streamProtocol.factory.continueTrying = 0 self._streamProtocol.transport.loseConnection() else: raise RuntimeError("not connected") def forwardMessage(self, msg): if msg[0] == "!": fwd_msg = "Unknown command" if msg == "!users": room = self.campfire.room(self.room.id) users = ', '.join([u['name'] for u in room.data['users']]) fwd_msg = "Active users: %s" % users elif msg == "!uploads": files = self.room.uploads() file_str = '\n'.join([f['full_url'].replace(' ', '%20') for f in files]) fwd_msg = "Uploaded files:\n%s" % file_str elif msg == "!room": fwd_msg = "Room URL: %s/room/%d" % (self.campfire.uri.geturl(), self.room.id) elif msg == "!leave": self.leave() fwd_msg = "Left room" elif msg == "!join": self.join() fwd_msg = "Joined room" elif msg == "!transcript": today = date.today() fwd_msg = "Transcript URL: %s/room/%d/transcript/%04d/%02d/%02d" % (self.campfire.uri.geturl(), self.room.id, today.year, today.month, today.day) self.xmpp.forwardMessage("# %s" % fwd_msg) else: if '\n' in msg: self.room.paste(msg) else: self.room.speak(msg) def join(self): if self.room and not self.joined: self.joined = True self.room.join() def leave(self): if self.room and self.joined: self.joined = False self.room.leave() def keepAlive(self): if self.room and self.joined: self.room.join()
class MyNotify(object): def __init__(self): super(MyNotify, self).__init__() # connect to Campfire self.c = Campfire(SUBDOMAIN, SECRET_TOKEN, ssl=True) if ROOM_ID: self.room = self.c.room(ROOM_ID) else: self.room = self.c.find_room_by_name(ROOM_NAME) self.room.join() print 'Begin...' self.room.listen(self.callback_for_campfire, self.error_callback) print 'End...' def callback_for_campfire(self, mes): print '***** Simple callback *****' print mes print '****** End callback *****' if mes['type']=='TextMessage': user = self.c.user(mes['user_id'])['user'] gravatar_hash = hashlib.md5(user['email_address'].lower()).hexdigest() title = user['name'] body = mes['body'] elif mes['type']=='TopicChangeMessage': user = self.c.user(mes['user_id'])['user'] gravatar_hash = hashlib.md5(user['email_address'].lower()).hexdigest() title = 'Topic has been changed' body = '%s changed the room’s topic to "%s"' % (user['name'], mes['body']) elif mes['type']=='LeaveMessage': user = self.c.user(mes['user_id'])['user'] gravatar_hash = hashlib.md5(user['email_address'].lower()).hexdigest() title = 'Someone has left the room' body = '%s has left the room' % user['name'] elif mes['type']=='EnterMessage': user = self.c.user(mes['user_id'])['user'] gravatar_hash = hashlib.md5(user['email_address'].lower()).hexdigest() title = 'Someone has joined the room' body = '%s has entered the room' % user['name'] elif mes['type']=='UploadMessage': user = self.c.user(mes['user_id'])['user'] gravatar_hash = hashlib.md5(user['email_address'].lower()).hexdigest() title = 'New file uploaded' body = '%s uploaded %s' % (user['name'], mes['body']) else: return n = pynotify.Notification(title, body) if gravatar_hash and SHOW_GRAVATAR: source = urllib.urlopen('http://www.gravatar.com/avatar/' + gravatar_hash) contents = source.read() get_image = gtk.gdk.PixbufLoader() get_image.write(contents) get_image.close() n.set_icon_from_pixbuf(get_image.get_pixbuf()) if PLAY_SOUND: pipeline = gst.Pipeline('mypipeline') audiotestsrc = gst.element_factory_make('audiotestsrc', 'audio') audiotestsrc.set_property('freq', FREQUENCY) pipeline.add(audiotestsrc) sink = gst.element_factory_make('alsasink', 'sink') pipeline.add(sink) audiotestsrc.link(sink) pipeline.set_state(gst.STATE_PLAYING) print '============== Sound ==============' n.show() if PLAY_SOUND: time.sleep(DURATION) pipeline.set_state(gst.STATE_READY) def error_callback(self, expt): print '***** Error callback *****' print expt print '***** End callback *****' n = pynotify.Notification('Whoops!', 'An error occurred', 'dialog-error') n.show()
class MyNotify(object): def __init__(self): super(MyNotify, self).__init__() # connect to Campfire self.c = Campfire(SUBDOMAIN, SECRET_TOKEN, ssl=True) if ROOM_ID: self.room = self.c.room(ROOM_ID) else: self.room = self.c.find_room_by_name(ROOM_NAME) self.room.join() print 'Begin...' self.room.listen(self.callback_for_campfire, self.error_callback) print 'End...' def callback_for_campfire(self, mes): print '***** Simple callback *****' print mes print '****** End callback *****' if mes['type']=='TextMessage': user = self.c.user(mes['user_id'])['user'] gravatar_hash = hashlib.md5(user['email_address'].lower()).hexdigest() title = user['name'] body = mes['body'] elif mes['type']=='TopicChangeMessage': user = self.c.user(mes['user_id'])['user'] gravatar_hash = hashlib.md5(user['email_address'].lower()).hexdigest() title = 'Topic has been changed' body = '%s changed the room’s topic to "%s"' % (user['name'], mes['body']) elif mes['type']=='LeaveMessage': user = self.c.user(mes['user_id'])['user'] gravatar_hash = hashlib.md5(user['email_address'].lower()).hexdigest() title = 'Someone has left the room' body = '%s has left the room' % user['name'] elif mes['type']=='EnterMessage': user = self.c.user(mes['user_id'])['user'] gravatar_hash = hashlib.md5(user['email_address'].lower()).hexdigest() title = 'Someone has joined the room' body = '%s has entered the room' % user['name'] elif mes['type']=='UploadMessage': user = self.c.user(mes['user_id'])['user'] gravatar_hash = hashlib.md5(user['email_address'].lower()).hexdigest() title = 'New file uploaded' body = '%s uploaded %s' % (user['name'], mes['body']) else: return n = pynotify.Notification(title, body) if gravatar_hash and SHOW_GRAVATAR: source = urllib.urlopen('http://www.gravatar.com/avatar/' + gravatar_hash) contents = source.read() get_image = gtk.gdk.PixbufLoader() get_image.write(contents) get_image.close() n.set_icon_from_pixbuf(get_image.get_pixbuf()) n.show() def error_callback(self, expt): print '***** Error callback *****' print expt print '***** End callback *****' n = pynotify.Notification('Whoops!', 'An error occurred', 'dialog-error') n.show()