Skip to content

Instantly share code, notes, and snippets.

@argon
Created October 15, 2014 15:45
Show Gist options
  • Save argon/127444b0f89fa796730a to your computer and use it in GitHub Desktop.
Save argon/127444b0f89fa796730a to your computer and use it in GitHub Desktop.
Bluemix/Node-RED demo from IBM Watson Tech Pioneer and Developer Day. Monitors tweets and routes through Watson Q&A API, with added translation
[
{
"id": "489498c.fb76b68",
"type": "watson-question-answer",
"name": "",
"output": "top",
"corpus": "healthcare",
"x": 469,
"y": 240,
"z": "17a16c06.e85e94",
"wires": [
[
"e3421933.1cbde8"
]
]
},
{
"id": "ab125e42.54eda",
"type": "watson-language-identification",
"name": "",
"x": 482,
"y": 44,
"z": "17a16c06.e85e94",
"wires": [
[
"39a44321.c65bbc"
]
]
},
{
"id": "40f5fe83.bf0a",
"type": "twitter in",
"twitter": "",
"tags": "",
"user": "true",
"name": "",
"topic": "tweets",
"x": 84,
"y": 44,
"z": "17a16c06.e85e94",
"wires": [
[
"f85a874a.07a578"
]
]
},
{
"id": "f85a874a.07a578",
"type": "function",
"name": "Filter tweet data",
"func": "// Ignore tweets from your demo account so we don't get stuck in a loop\nif (msg.user == \"<test twitter handle>\") return null;\n\n// Remove the username from the tweet.\nmsg.payload = msg.payload.replace(/\\@\\S+\\s*/i, \"\");\n\nreturn msg;",
"outputs": 1,
"x": 270,
"y": 44,
"z": "17a16c06.e85e94",
"wires": [
[
"ab125e42.54eda"
]
]
},
{
"id": "39a44321.c65bbc",
"type": "function",
"name": "Check Language",
"func": "// Keep track of language for translating back later\nmsg.language = msg.lang\n\nif (msg.lang == \"en-US\") {\n\t// If the question was in english then return it on first output\n return [msg,null];\n}\nelse {\n\t// otherwise turn it into a code for the machine\n\t// translation service to translate to english\n\t// Format: \"mt-<from code>-enus\"\n msg.lang = \"mt-\" + msg.lang.toLowerCase().replace(/\\-/, \"\") + \"-enus\"\n return [null, msg];\n}",
"outputs": "2",
"x": 148,
"y": 186,
"z": "17a16c06.e85e94",
"wires": [
[
"489498c.fb76b68"
],
[
"986364da.679c98"
]
]
},
{
"id": "986364da.679c98",
"type": "watson-translate",
"name": "",
"language": "",
"x": 171,
"y": 290,
"z": "17a16c06.e85e94",
"wires": [
[
"489498c.fb76b68"
]
]
},
{
"id": "42952348.bd6adc",
"type": "function",
"name": "Convert to a \"tweet\"",
"func": "// Add username to the message and trim to 140chars\nmsg.payload = (\"@\" + msg.user + \" \" + msg.payload).substr(0, 140);\nreturn msg;",
"outputs": 1,
"x": 613,
"y": 494,
"z": "17a16c06.e85e94",
"wires": [
[
"b7f2316c.480dd"
]
]
},
{
"id": "b7f2316c.480dd",
"type": "twitter out",
"twitter": "",
"name": "Tweet",
"x": 786,
"y": 494,
"z": "17a16c06.e85e94",
"wires": []
},
{
"id": "e3421933.1cbde8",
"type": "function",
"name": "Translate back to original language",
"func": "if (msg.lang == \"en-US\") {\n return [msg,null];\n}\nelse {\n msg.lang = \"mt-enus-\" + msg.language.toLowerCase().replace(/\\-/, \"\")\n return [null, msg];\n}",
"outputs": "2",
"x": 198,
"y": 502,
"z": "17a16c06.e85e94",
"wires": [
[
"42952348.bd6adc"
],
[
"9d4b21cc.62b4e"
]
]
},
{
"id": "9d4b21cc.62b4e",
"type": "watson-translate",
"name": "",
"language": "",
"x": 455,
"y": 544,
"z": "17a16c06.e85e94",
"wires": [
[
"42952348.bd6adc"
]
]
}
]
@hquillaud
Copy link

Somehow msg.user did not work for me on the 'Convert to a "tweet"'node. I modified the 'twitter in' node to monitor direct messages and used msg.tweet.sender.screen_name on the 'Convert to a "tweet"'node, and that worked. Great demo, even if the Watson translation to French could be improved upon. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment