def send_system_notification(notification, topic, recipient): """ Send out a system notification via PubNub. The parameters are as follows: 'notification' The text of the notification to send out. 'topic' The Topic object this notification is about. 'recipient' The User this notification should be sent to. We send out the given notification, about the given topic, to the given user. """ topic_id = topic.id recipient_id = recipient.id created_at = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ") message_body = notification if settings.ENABLE_PUBNUB: channel = "message-" + str(topic_id) + "-" + str(recipient_id) payload = json.dumps({'sender_id' : None, 'recipient_id' : recipient_id, 'created_at' : created_at, 'message_body' : message_body}) p = Pubnub(publish_key=settings.PUBNUB_PUBLISH_KEY, subscribe_key=settings.PUBNUB_SUBSCRIBE_KEY, secret_key=settings.PUBNUB_SECRET_KEY) results = p.publish({'channel' : channel, 'message' : payload}) signals.pubnub_system_notification_sent.send(sender=None, notification=notification, topic=topic, recipient=recipient) logger.debug('pubnub.send_system_notification("%s", %d, %d)' % (notification, topic_id, recipient_id))
def send_notification(message): """ Send out a notification about the given message via PubNub. 'message' should be a Message object. We send out the given message via the PubNub notification service. When sending out a notification, the PubNub channel name will have the following format: message-[topic_id]-[recipient_id] where "[topic_id]" is the record ID of the topic, and "[recipient_id]" is the record ID of the receiving user. The payload associated with the PubNub notification will be a JSON-format object with the following fields: 'sender_id' The record ID of the message sender. 'recipient_id' The record ID of the message recipient. 'created_at' The date and time at which the message was created, as a string in ISO 8601 format. 'message_body' The contents of the message. """ sender_id = message.sender.id topic_id = message.conversation.topic.id created_at = message.created_at.strftime("%Y-%m-%dT%H:%M:%SZ") message_body = message.body if message.conversation.user_1 == message.sender: recipient_id = message.conversation.user_2.id else: recipient_id = message.conversation.user_1.id if settings.ENABLE_PUBNUB: channel = "message-" + str(topic_id) + "-" + str(recipient_id) payload = json.dumps({'sender_id' : sender_id, 'recipient_id' : recipient_id, 'created_at' : created_at, 'message_body' : message_body}) p = Pubnub(publish_key=settings.PUBNUB_PUBLISH_KEY, subscribe_key=settings.PUBNUB_SUBSCRIBE_KEY, secret_key=settings.PUBNUB_SECRET_KEY) results = p.publish({'channel' : channel, 'message' : payload}) signals.pubnub_notification_sent.send(sender=None, message=message) logger.debug('pubnub.send_notification(%d, %d, %d, "%s")' % (topic_id, sender_id, recipient_id, message_body))
def send_message_to_firehose(message): """ Send the given message to the messageme firehose. 'message' should be a Message object. We send out the given message via the PubNub notification service to the messageme firehose. We send a PubNub notification to the channel named "message-firehose". The payload associated with the PubNub notification will be a JSON-format object with the following fields: 'message_id' The record ID of the message that is being sent. 'conversation_id' The record ID of the conversation that this message is part of. 'domain' The name of the site this message is being sent through. One of: "message.me" "message.us" 'from_user_id' The record ID of the user that is sending this message. This will always uniquely identify the user, even if a name or phone number hasn't been entered. 'from_name' The name of the user that is sending this message. Note that this will be blank if the user hasn't entered a name. 'from_phone_number' The phone number that this message is from. Note that this will be blank if the user hasn't entered a phone number. 'to_user_id' The record ID of the user that this message is being sent to. This will always uniquely identify the user, even if a name or phone number hasn't been entered. 'to_name' The name of the user that this message is being sent to. Note that this will be blank if the user hasn't entered a name. 'to_phone_number' The phone number that this message is being sent to. Note that this will be blank if the user hasn't entered a phone number. 'topic_id' The record ID of the topic this conversation is about. 'topic_name' The name of the topic this conversation is about. Note that this will be blank if the conversation is about the user's default topic. 'timestamp' The date and time at which this message was sent, in ISO 8601 format in UTC (ie, "YYYY-MM-DDTHH:MM:SSZ"). 'body' The body of the message, as a string. 'private' A boolean indicating whether or not the message is private. Note that at present all messages are private. """ domain = "message.me" # Hardwired for now... user_1 = message.conversation.user_1 user_2 = message.conversation.user_2 if message.sender == user_1: sender_id = user_1.id if user_1.username not in ["", None]: sender_name = user_1.username elif message.sender_name not in ["", None]: sender_name = message.sender_name else: sender_name = "" if user_1.phone_number not in ["", None]: sender_phone_number = user_1.phone_number else: sender_phone_number = "" recipient_id = user_2.id if user_2.username not in ["", None]: recipient_name = user_2.username else: recipient_name = "" if user_2.phone_number not in ["", None]: recipient_phone_number = user_2.phone_number else: recipient_phone_number = "" else: sender_id = user_2.id if user_2.username not in ["", None]: sender_name = user_2.username elif message.sender_name not in ["", None]: sender_name = message.sender_name else: sender_name = "" if user_2.phone_number not in ["", None]: sender_phone_number = user_2.phone_number else: sender_phone_number = "" recipient_id = user_1.id if user_1.username not in ["", None]: recipient_name = user_1.username else: recipient_name = "" if user_1.phone_number not in ["", None]: recipient_phone_number = user_1.phone_number else: recipient_phone_number = "" if message.conversation.topic.name not in ["", None]: topic_name = message.conversation.topic.name else: topic_name = "" timestamp = message.created_at.strftime("%Y-%m-%dT%H:%M:%SZ") private = True # Hardwired for now. payload = {'message_id' : message.id, 'conversation_id' : message.conversation.id, 'domain' : domain, 'from_user_id' : sender_id, 'from_name' : sender_name, 'from_phone_number' : sender_phone_number, 'to_user_id' : recipient_id, 'to_name' : recipient_name, 'to_phone_number' : recipient_phone_number, 'topic_id' : message.conversation.topic.id, 'topic_name' : topic_name, 'timestamp' : timestamp, 'body' : message.body, 'private' : private} if settings.ENABLE_PUBNUB: channel = "messageme-firehose" payload = json.dumps(payload) p = Pubnub(publish_key=settings.PUBNUB_PUBLISH_KEY, subscribe_key=settings.PUBNUB_SUBSCRIBE_KEY, secret_key=settings.PUBNUB_SECRET_KEY) results = p.publish({'channel' : channel, 'message' : payload}) signals.pubnub_firehose_notification_sent.send(sender=None, message=message) logger.debug('pubnub.send_message_to_firehose(%d, "%s")' % (message.id, message.body))