Skip to content

Instantly share code, notes, and snippets.

@mheadd
Created May 27, 2011 19:50
Show Gist options
  • Select an option

  • Save mheadd/996012 to your computer and use it in GitHub Desktop.

Select an option

Save mheadd/996012 to your computer and use it in GitHub Desktop.
PHP script and CouchDB design document elements for an SMS management system
<?php
// Include required classes.
require 'classes/sag/Sag.php';
require 'classes/smsified/inbound.class.php';
require 'classes/smsified/smsified.class.php';
// Extend InboundMessage class.
class SMSMessage extends InboundMessage {
public function __construct($json) {
parent::__construct($json);
}
// fucntion to create JSON document for storing in CouchDB.
public function __toString() {
$message = array();
$message["timestamp"] = $this->timeStamp;
$message["destinationAddress"] = $this->destinationAddress;
$message["message"] = $this->message;
$message["senderAddress"] = $this->senderAddress;
return json_encode($message);
}
}
// Insert an inbound text message in CouchDB.
try {
// Grab the raw JSON sent from SMSified.
$json = file_get_contents("php://input");
$SMS = new SMSMessage($json);
// Create a new Sag instance.
$sag = new Sag('127.0.0.1', 5984);
$sag->setDatabase("smsrecords");
// Save the SMS message in CouchDB.
$id = $SMS->getMessageId();
$sag->put($id, sprintf($SMS));
}
catch (SagCouchException $ex) {
die("*** ".$ex->getMessage()." ***");
}
{
"rss": "function(head, req) { var row; var date = new Date(); start({\"headers\":{\"Content-type\": \"application/rss+xml\"}}); var out = '<?xml version=\"1.0\"?><rss version=\"2.0\"><channel>'; out += '<title>Inbound SMS Messages</title>'; out += '<description>A List of all inbound SMS messages</description>'; out += '<lastBuildDate>' + date.toGMTString() + '</lastBuildDate>'; out += '<pubDate>' + date.toGMTString() + '</pubDate>'; while (row = getRow()) { out +='<item><title>Inbound Text Message from ' + row.value.title + '</title><description>' + row.value.description + '</description><guid>' + row.value.guid + '</guid><link>http://textmanager:5984/' + row.id + '</link><pubDate>' + row.value.pubDate + '</pubDate></item>'; } out += '</channel></rss>'; send(out); }"
}
{
"details": {
"map": "function(doc) {\n emit(doc._id, {title: doc.senderAddress, description: doc.message, guid: doc._id, pubDate: doc.timestamp });\n}"
}
}
[
{
"from": "/smsfeed",
"to": "_list/rss/details"
},
{
"from": "/:id",
"to": "_show/details/:id"
}
]
{
"details": "function(doc, req) { return '<h2>Inbound Text Message from ' + doc.senderAddress + '</h2>' + '<p> Message: ' + doc.message + '<br/>Sent: ' + doc.timestamp + '</p>'}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment