This is a flow that loads emails from a mailbox at yandex, filters the mails on the date, translates the email if it is in Swedish to English and then does sentiment analysis with the Google language API.

Nodes that has to be installed to use this flow:

  • node-red-contrib-html-to-text
  • node-red-node-email
  • node-red-contrib-filter
  • node-red-contrib-google-cloud (translate and sentiment)
  • node-red-contrib-mail-actions

If the sentiment score is lower than -0.3 the email will be placed in the mail folder Urgent. The magnitude would also be good to check as messages that include both + and - sentiment scores can cancel each other out giving an overall score of 0. A high magnitude can indicate that. The date filtering is done to prevent too many emails when testing.

The flow is started by clicking on the first node which is an inject node. This could be changed to have it triggered in a time interval instead and the mail node, set to only get the latest unread emails.

For the setup of Google cloud nodes you can follow the excellent instructions at the github for the node-red-contrib-google-cloud node.

Copy the flow below and import it to your apivis editor.

[{"id":"971b8b9b.c27a38","type":"tab","label":"email sentiment analysis","disabled":false,"info":""},{"id":"f27816b9.3077c8","type":"google-cloud-language-sentiment","z":"971b8b9b.c27a38","account":"","keyFilename":"","languageCode":"en","name":"","x":580,"y":280,"wires":[["81127a0e.7848d8","adbad8fe.395698"]]},{"id":"b560bba0.50c758","type":"e-mail in","z":"971b8b9b.c27a38","name":"","protocol":"IMAP","server":"imap.yandex.com","useSSL":true,"port":"993","box":"Inbox","disposition":"None","criteria":"ALL","repeat":"300","fetch":"trigger","inputs":1,"x":230,"y":80,"wires":[["cb282f10.5b54a"]]},{"id":"597f543d.7635ac","type":"google-cloud-translate","z":"971b8b9b.c27a38","account":"","keyFilename":"","projectId":"","sourceLanguageCode":"sv","targetLanguageCode":"en","name":"","x":140,"y":280,"wires":[["a5bb9da.728a56"]]},{"id":"a5bb9da.728a56","type":"change","z":"971b8b9b.c27a38","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[\t   {\t       \"\":msg.payload.translations.translatedText\t   }\t]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":280,"wires":[["9aae418d.4a6bc"]]},{"id":"9aae418d.4a6bc","type":"json","z":"971b8b9b.c27a38","name":"","property":"payload","action":"","pretty":false,"x":450,"y":280,"wires":[["f27816b9.3077c8"]]},{"id":"649b2c7b.9eefc4","type":"inject","z":"971b8b9b.c27a38","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":80,"wires":[["b560bba0.50c758"]]},{"id":"ec5a61c2.56731","type":"filter","z":"971b8b9b.c27a38","name":"filter date","property":"payload","propertyType":"msg","asArray":false,"itemProperty":"","itemPropertyType":"item","rules":[{"t":"cont","v":"Jan","vt":"str","output":1}],"checkall":"true","outputs":1,"x":560,"y":80,"wires":[["73bb0264.71c55c"]]},{"id":"cb282f10.5b54a","type":"template","z":"971b8b9b.c27a38","name":"pass date to filter","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{date}}","output":"str","x":390,"y":80,"wires":[["ec5a61c2.56731"]]},{"id":"73bb0264.71c55c","type":"filter","z":"971b8b9b.c27a38","name":"filter year","property":"payload","propertyType":"msg","asArray":false,"itemProperty":"","itemPropertyType":"item","rules":[{"t":"cont","v":"2020","vt":"str","output":1}],"checkall":"true","outputs":1,"x":700,"y":80,"wires":[["3cde58d4.002418","3b0579ac.adfa66"]]},{"id":"3cde58d4.002418","type":"change","z":"971b8b9b.c27a38","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"html","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":160,"y":200,"wires":[["ba9ba553.cf85c8"]]},{"id":"ba9ba553.cf85c8","type":"html-to-text","z":"971b8b9b.c27a38","name":"","x":330,"y":200,"wires":[["83ab1.e45d354f"]]},{"id":"3b0579ac.adfa66","type":"change","z":"971b8b9b.c27a38","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"topic","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":160,"y":140,"wires":[["90b36504.a307a8"]]},{"id":"83ab1.e45d354f","type":"change","z":"971b8b9b.c27a38","name":"","rules":[{"t":"set","p":"parts","pt":"msg","to":"2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":200,"wires":[["bc9926c3.f43f08"]]},{"id":"bc9926c3.f43f08","type":"join","z":"971b8b9b.c27a38","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":690,"y":200,"wires":[["597f543d.7635ac"]]},{"id":"90b36504.a307a8","type":"change","z":"971b8b9b.c27a38","name":"","rules":[{"t":"set","p":"parts","pt":"msg","to":"1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":140,"wires":[["bc9926c3.f43f08"]]},{"id":"e932b8fd.6414e8","type":"mailactions","z":"971b8b9b.c27a38","name":"urgent","protocol":"IMAP","server":"imap.yandex.com","useSSL":true,"port":"993","box":"INBOX","outbox":"urgent","tag":"","x":610,"y":360,"wires":[["75763331.fe6f4c"]]},{"id":"81127a0e.7848d8","type":"switch","z":"971b8b9b.c27a38","name":"if sentiment is < -0.3 place the message in urgent folder","property":"sentiment.score","propertyType":"msg","rules":[{"t":"lt","v":"-0.3","vt":"num"},{"t":"gte","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":290,"y":360,"wires":[["e932b8fd.6414e8"],["3f3d783b.c7bfd8"]]},{"id":"75763331.fe6f4c","type":"debug","z":"971b8b9b.c27a38","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":360,"wires":[]},{"id":"adbad8fe.395698","type":"debug","z":"971b8b9b.c27a38","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"sentiment.score","targetType":"msg","x":780,"y":280,"wires":[]},{"id":"22a0cd2d.218592","type":"comment","z":"971b8b9b.c27a38","name":"","info":"This is a flow that loads emails from a mailbox at yandex, filters the mails on the date, translates the email if it is in Swedish to English and then does sentiment analysis with the Google language API.\nIf the source language field is empty it will detect the language but could be unstable as you will get a warning. It works but I get some strange behavior and probably can crash the server.\n\nNodes that has to be installed to use this flow:\n\n- node-red-contrib-html-to-text\n- node-red-node-email\n- node-red-contrib-filter\n- node-red-contrib-google-cloud (translate and sentiment)\n- node-red-contrib-mail-actions\n\nIf the sentiment score is lower than -0.3 the email will be placed in the mail folder Urgent. \nThe magnitude would also be good to check as messages that include both + and - sentiment scores can cancel each other out giving an overall score of 0. \nA high magnitude can indicate that. \nThe date filtering is done to prevent too many emails when testing. \n\nThe flow is started by clicking on the first node which is an inject node. This could be changed to have it triggered in a time interval instead and the mail node, set to only get the latest unread emails.\n\n","x":100,"y":40,"wires":[]},{"id":"3f3d783b.c7bfd8","type":"debug","z":"971b8b9b.c27a38","name":"Do nothing","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":630,"y":400,"wires":[]}]