Twitter Sentiment Trend Analysis

Uses Twitter, Sentiment analysis, MongoDB, Twilio and even the IOT output to send the running average to a realtime chart: https://quickstart.internetofthings.ibmcloud.com/#/device/987abc654def/sensor/sentiment

[{"id":"26cf76b6.07554a","type":"twilio-api","sid":"ACc25c81b3506df8830a32f65b07cf2b87","from":"+16042278447","name":"SMS"},{"id":"479044ce.b86fbc","type":"function","name":"calculate average sentiment","func":"var sentimentData = msg.payload; //set sentimentData to the contents of cloudant query\n\nvar totalSentiment;\ntotalSentiment = sentimentData[0].total;\n/*\nif (totalSentiment == null){  //if there are no records in cloudant\n\ttotalSentiment = 0;  //then set total to 0\n}\n*/\ntotalSentiment = totalSentiment + msg.sentiment.score;\n\nvar recordCount;\nrecordCount = sentimentData[0].count;\n/*\nif (totalRecords == null){  //if there are non records in cloudant\n\ttotalRecords = 0;  //then set oldCount to 0\n}\n*/\nrecordCount = recordCount + 1;\n\nvar avg;\navg = totalSentiment/recordCount;  //set newAvg to be the result of the oldAvg divided by the new count\n\nmsg.totalSentiment = totalSentiment;\nmsg.recordCount = recordCount;\nmsg.avg = avg;\n\nreturn msg;","outputs":1,"x":470,"y":363,"z":"a304527d.5cfbb","wires":[["aa55bfbd.55aa4","daa53b0d.255ac8","b4244b6c.4bdbb8"]]},{"id":"8db42322.724be","type":"mongodb out","service":"MongoLab-pk","mongodb":"","name":"mongo update","collection":"sentiment","payonly":false,"upsert":true,"multi":false,"operation":"update","x":774,"y":567,"z":"a304527d.5cfbb","wires":[]},{"id":"2890d9e6.d76f26","type":"sentiment","name":"","x":215,"y":160,"z":"a304527d.5cfbb","wires":[["69b005e9.964ffc"]]},{"id":"69b005e9.964ffc","type":"mongodb in","service":"MongoLab-pk","mongodb":"","name":"mongo query","collection":"sentiment","operation":"find","x":337,"y":258,"z":"a304527d.5cfbb","wires":[["479044ce.b86fbc"]]},{"id":"ccfdf3ae.33021","type":"debug","name":"","active":true,"console":"false","complete":"recordCount","x":899,"y":409,"z":"a304527d.5cfbb","wires":[]},{"id":"b4244b6c.4bdbb8","type":"debug","name":"","active":true,"console":"false","complete":"avg","x":846,"y":367,"z":"a304527d.5cfbb","wires":[]},{"id":"57562d96.a8a9d4","type":"debug","name":"","active":true,"console":"false","complete":"totalSentiment","x":928,"y":450,"z":"a304527d.5cfbb","wires":[]},{"id":"76383c73.89c7c4","type":"function","name":"build chart output","func":"var sentimentData = msg.payload; //set sentimentData to the contents of cloudant query\n\nvar avg;\navg = sentimentData[0].average;\n\nmsg.payload = JSON.stringify({\n   d: {\n       myName: \"Sentiment Score\",\n       sentiment: avg\n   }\n});\n\nreturn msg;","outputs":1,"x":296,"y":448,"z":"a304527d.5cfbb","wires":[["414207a9.bebdf8"]]},{"id":"414207a9.bebdf8","type":"ibmiot out","authentication":"quickstart","apiKey":"","outputType":"evt","deviceId":"987abc654def","deviceType":"0.10.4","eventCommandType":"sentiment","format":"json","data":"__override__","name":"average sentiment","service":"quickstart","x":448,"y":527,"z":"a304527d.5cfbb","wires":[]},{"id":"5a76728c.a5898c","type":"mongodb in","service":"MongoLab-pk","mongodb":"","name":"mongo query","collection":"sentiment","operation":"find","x":161,"y":357,"z":"a304527d.5cfbb","wires":[["76383c73.89c7c4"]]},{"id":"3a881b20.c577e4","type":"inject","name":"5 second timer","topic":"","payload":"","payloadType":"date","repeat":"5","crontab":"","once":false,"x":95,"y":261,"z":"a304527d.5cfbb","wires":[["5a76728c.a5898c"]]},{"id":"ecd8e3c5.13272","type":"inject","name":"reset database","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":120.5,"y":646,"z":"a304527d.5cfbb","wires":[["fe90947e.016f68"]]},{"id":"fe90947e.016f68","type":"function","name":"build mongo reset msg","func":"var newMsg = {\n\tpayload:{\n\ttotal:0,\n\tcount:0,\n\taverage:0\n\t}\n};  //build new message payload\n\nreturn newMsg;","outputs":1,"x":384,"y":626,"z":"a304527d.5cfbb","wires":[["8db42322.724be"]]},{"id":"aa55bfbd.55aa4","type":"switch","name":"Average sentiment less than -1?","property":"avg","rules":[{"t":"lt","v":"-1"}],"checkall":"true","outputs":1,"x":778,"y":267,"z":"a304527d.5cfbb","wires":[["a9363d64.56c9c"]]},{"id":"a9363d64.56c9c","type":"function","name":"Build SMS message","func":"msg.payload = 'Avg Sentiment ' + msg.avg + ' https://quickstart.internetofthings.ibmcloud.com/#/device/987abc654def/sensor/sentiment';\nreturn msg;","outputs":1,"x":927.5,"y":173,"z":"a304527d.5cfbb","wires":[["a810da2d.57ef28"]]},{"id":"a810da2d.57ef28","type":"twilio out","service":"_ext_","twilio":"26cf76b6.07554a","from":"","number":"+17782282586","name":"SMS Alert","x":971.25,"y":75.75,"z":"a304527d.5cfbb","wires":[]},{"id":"daa53b0d.255ac8","type":"function","name":"build mongo msg","func":"var newMsg = {\n\tpayload:{\n\ttotal:msg.totalSentiment,\n\tcount:msg.recordCount,\n\taverage:msg.avg\n\t}\n};  //build new message payload\n\n\nreturn newMsg;","outputs":1,"x":654,"y":436,"z":"a304527d.5cfbb","wires":[["8db42322.724be"]]},{"id":"493d4e4b.b6c2b","type":"twitter in","twitter":"","tags":"lululemon","user":"false","name":"","topic":"tweets","x":113,"y":76,"z":"a304527d.5cfbb","wires":[["2890d9e6.d76f26","c2645078.3d9bb"]]},{"id":"c2645078.3d9bb","type":"debug","name":"","active":true,"console":"false","complete":"false","x":321,"y":44,"z":"a304527d.5cfbb","wires":[]}]
peterhsburton

Flow Info

created 2 years, 9 months ago

Node Types

Core
  • debug (x4)
  • function (x5)
  • inject (x2)
  • mongodb in (x2)
  • mongodb out (x1)
  • sentiment (x1)
  • switch (x1)
  • twitter in (x1)
Other
  • ibmiot out (x1)
  • twilio out (x1)
  • twilio-api (x1)

Tags

  • peterhsburton
  • twitter
  • sentiment
  • twilio
  • iot
  • mongo
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option