Circuit Chat Bot Example

Circuit chat bot for a given conversation on https://circuitsandbox.net/. This example has three separate functionalities if typed in the conversation.

-"/joke" The bot will tell a Chuck Norris joke from https://geek-jokes.sameerkumar.website/api.

-"/weather@{zip_code},{optional_country_code}" or "/weather@{city_name},{optional_country_code}" Will provide weather data on the given city from https://openweathermap.org, formatting for codes can be found on their website. For example, "/weather@Toronto".

-"/define-{language_code},{word}" or "/define-{word} Will provide data/definition on the given word from https://developer.oxforddictionaries.com, if no language code is given will default to English. For example, "/define-en,dog" will give the definition of "dog" in the english language. Further information on language codes can be found on their web site.

Requires: node-red, node-red-contrib-circuit, node-red-contrib-http-request.

[{"id":"6c90486b.a59908","type":"Events","z":"3801caef.84af06","server":"d22bd24e.dd605","callEvent":true,"convEvent":true,"itemEvent":true,"userEvent":true,"name":"evt","x":130,"y":820,"wires":[["d899ea64.9162e8"]]},{"id":"d899ea64.9162e8","type":"switch","z":"3801caef.84af06","name":"","property":"payload.type","propertyType":"msg","rules":[{"t":"eq","v":"itemAdded","vt":"str"},{"t":"eq","v":"itemUpdated","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":230,"y":820,"wires":[["5035c104.e6498"],["5035c104.e6498"]]},{"id":"f1e77a5a.426f58","type":"switch","z":"3801caef.84af06","name":"send api request","property":"payload.item.text.content","propertyType":"msg","rules":[{"t":"cont","v":"/joke","vt":"str"},{"t":"cont","v":"/weather@","vt":"str"},{"t":"cont","v":"/define-","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":490,"y":820,"wires":[["83f22770.418ad8"],["71b83116.0737b"],["25134e58.90ed12"]]},{"id":"5035c104.e6498","type":"switch","z":"3801caef.84af06","name":"","property":"payload.item.convId","propertyType":"msg","rules":[{"t":"eq","v":"1bf601e2-affc-4869-9b05-3715b5bb4751","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":350,"y":820,"wires":[["f1e77a5a.426f58"]]},{"id":"2f0d7426.612d5c","type":"addTextItem","z":"3801caef.84af06","server":"d22bd24e.dd605","convId":"1bf601e2-affc-4869-9b05-3715b5bb4751","name":"sendJoke","x":1200,"y":740,"wires":[[]]},{"id":"bb2d0ea2.30757","type":"function","z":"3801caef.84af06","name":"msg.","func":"const content = msg.payload;\n// const convId = '1bf601e2-affc-4869-9b05-3715b5bb4751';\nmsg.payload = {\n    content: content\n}\nreturn msg;","outputs":1,"noerr":0,"x":1070,"y":720,"wires":[["2f0d7426.612d5c"]]},{"id":"83f22770.418ad8","type":"www-request","z":"3801caef.84af06","name":"joke request","method":"GET","ret":"txt","url":"https://geek-jokes.sameerkumar.website/api","follow-redirects":true,"tls":"","x":710,"y":760,"wires":[["bd62872.87c2a78"]]},{"id":"6e79949e.7946cc","type":"json","z":"3801caef.84af06","name":"","property":"payload","action":"obj","pretty":false,"x":950,"y":720,"wires":[["bb2d0ea2.30757"]]},{"id":"399361.fc538ca","type":"www-request","z":"3801caef.84af06","name":"weather request","method":"GET","ret":"txt","url":"","follow-redirects":true,"tls":"","x":1000,"y":820,"wires":[["ea1de8a4.de1bf8"]]},{"id":"71b83116.0737b","type":"function","z":"3801caef.84af06","name":"parseWeatherContent","func":"const APPID = '067e84d784f2984b49e1bfef169ab684'; // API id from openweathermap.org, go to site to get your own.\nconst content = msg.payload.item.text.content;\nconst index = content.indexOf('/weather@');\nlet url = null;\nif (index > -1) {\n    const query = content.substring(index + 9, content.length).split(' ');\n    if (/\\d/.test(query)) {\n        url = `https://api.openweathermap.org/data/2.5/weather?zip=${query}&APPID=${APPID}`;\n    } else {\n        let q = encodeURI(query);\n        url = `https://api.openweathermap.org/data/2.5/weather?q=${q}&APPID=${APPID}`;\n    }\n}\nmsg.url = url;\nreturn msg;","outputs":1,"noerr":0,"x":720,"y":820,"wires":[["15870dc0.7b7e62"]]},{"id":"ea1de8a4.de1bf8","type":"json","z":"3801caef.84af06","name":"","property":"payload","action":"obj","pretty":false,"x":1150,"y":800,"wires":[["eb707b4a.0b2b58"]]},{"id":"15870dc0.7b7e62","type":"switch","z":"3801caef.84af06","name":"","property":"url","propertyType":"msg","rules":[{"t":"nnull"}],"checkall":"true","repair":false,"outputs":1,"x":870,"y":820,"wires":[["399361.fc538ca"]]},{"id":"5cd5b5ec.559ccc","type":"function","z":"3801caef.84af06","name":"parseWeatherData","func":"const content = msg.payload;\nfunction convert(temp) {\n    return ((temp - 273.15) * (1.8) + 32).toFixed(2);\n}\nlet payload = {\n    content: `${content.name}: ${content.weather && content.weather[0].description}.\n        Current Temperature: ${convert(content.main.temp)} &deg;F\n            High: ${convert(content.main.temp_max)} &deg;F\n            Low: ${convert(content.main.temp_min)} &deg;F\n        Humidity: ${content.main.humidity}\n        Pressure: ${content.main.pressure}`\n}\nmsg.payload = payload;\nreturn msg;","outputs":1,"noerr":0,"x":1390,"y":800,"wires":[["5a1adc6.48da424"]]},{"id":"5a1adc6.48da424","type":"addTextItem","z":"3801caef.84af06","server":"d22bd24e.dd605","convId":"1bf601e2-affc-4869-9b05-3715b5bb4751","name":"AddWeather","x":1570,"y":820,"wires":[[]]},{"id":"eb707b4a.0b2b58","type":"switch","z":"3801caef.84af06","name":"","property":"statusCode","propertyType":"msg","rules":[{"t":"eq","v":"200","vt":"str"},{"t":"eq","v":"404","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":1250,"y":800,"wires":[["5cd5b5ec.559ccc"],["22fad4a0.d9c40c"]]},{"id":"22fad4a0.d9c40c","type":"function","z":"3801caef.84af06","name":"404","func":"msg.payload.content = 'City was not found...';\nreturn msg;","outputs":1,"noerr":0,"x":1390,"y":840,"wires":[["5a1adc6.48da424"]]},{"id":"25134e58.90ed12","type":"function","z":"3801caef.84af06","name":"parseDefRequest","func":"const headers = { // for dictionary api\n  Accept: 'application/json',\n  app_id:'f7086446', // go to developer.oxforddictionaries.com to get your own\n  app_key: '342b5dbaf1d5be31c4da9dedb96a09d2' // go to developer.oxforddictionaries.com to get your own\n}\n\nconst content = msg.payload.item.text.content;\nconst index = content.indexOf('/define-');\nlet url = null;\nif (index > -1) {\n    let query = content.substring(index + 8, content.length).split(' ')[0].replace(/<\\/?[^>]+(>|$)/g, '');\n    if (query.includes(',')) {\n        query = query.replace(',', '/');\n        url = `https://od-api.oxforddictionaries.com:443/api/v1/entries/${query}`;\n    } else { // defaults to english if not specified\n        url = `https://od-api.oxforddictionaries.com:443/api/v1/entries/en/${query}`;\n    }\n    \n\n}\nmsg.url = url;\nmsg.headers = headers;\nreturn msg;","outputs":1,"noerr":0,"x":710,"y":900,"wires":[["3e917b8b.699af4"]]},{"id":"11cece7e.ee2e22","type":"www-request","z":"3801caef.84af06","name":"defRequest","method":"GET","ret":"txt","url":"","follow-redirects":true,"tls":"","x":970,"y":900,"wires":[["33e0264f.5b9fba"]]},{"id":"3e917b8b.699af4","type":"switch","z":"3801caef.84af06","name":"","property":"url","propertyType":"msg","rules":[{"t":"nnull"}],"checkall":"true","repair":false,"outputs":1,"x":850,"y":900,"wires":[["11cece7e.ee2e22"]]},{"id":"33e0264f.5b9fba","type":"switch","z":"3801caef.84af06","name":"","property":"statusCode","propertyType":"msg","rules":[{"t":"eq","v":"200","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1090,"y":900,"wires":[["f4227695.8f87e8"],["571676.7993298c"]]},{"id":"aa1ae50f.bca548","type":"function","z":"3801caef.84af06","name":"parseDefinition","func":"let content = ``;\nconst res = msg.payload.results;\nres.forEach(r => {\n    content += `${r.word}: \\n`;\n    r.lexicalEntries.forEach(entry => {\n        let i = 1;\n        content += `Type: ${entry.lexicalCategory} \\n`\n        entry.entries.forEach(e => {\n            e.senses && e.senses.forEach(s => {\n                s.definitions.forEach(def => content += `\\t ${i++}. ${def}.\\n`);\n            });\n        });\n    });\n});\nmsg.payload.content = content;\nreturn msg;","outputs":1,"noerr":0,"x":1340,"y":880,"wires":[["468fafa3.8de65"]]},{"id":"571676.7993298c","type":"function","z":"3801caef.84af06","name":"404","func":"const message = {\n    payload: {\n        content: 'There was an error looking up your word...'\n    }\n}\nreturn message;","outputs":1,"noerr":0,"x":1270,"y":920,"wires":[["468fafa3.8de65"]]},{"id":"468fafa3.8de65","type":"addTextItem","z":"3801caef.84af06","server":"d22bd24e.dd605","convId":"1bf601e2-affc-4869-9b05-3715b5bb4751","name":"addDefinition","x":1510,"y":920,"wires":[[]]},{"id":"f4227695.8f87e8","type":"json","z":"3801caef.84af06","name":"","property":"payload","action":"obj","pretty":false,"x":1210,"y":880,"wires":[["aa1ae50f.bca548"]]},{"id":"bd62872.87c2a78","type":"switch","z":"3801caef.84af06","name":"","property":"statusCode","propertyType":"msg","rules":[{"t":"eq","v":"200","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":830,"y":760,"wires":[["6e79949e.7946cc"],["51fcb4fe.ca2a0c"]]},{"id":"51fcb4fe.ca2a0c","type":"function","z":"3801caef.84af06","name":"404","func":"msg.payload.content = 'There was an error telling your joke...';\nreturn msg;","outputs":1,"noerr":0,"x":990,"y":780,"wires":[["2f0d7426.612d5c"]]},{"id":"d22bd24e.dd605","type":"circuit-server","z":"","domain":"circuitsandbox.net","clientid":"98594cb4951240ce8f65944d1ea07f46","clientsecret":"b43e8a83de8040cabd97fea2478631f4","allowScope":false,"scope":"READ_CONVERSATIONS","loglevel":"Error"}]

Flow Info

Created 6 years, 2 months ago
Rating: not yet rated

Actions

Rate:

Node Types

Core
  • function (x8)
  • json (x3)
  • switch (x8)
Other
  • Events (x1)
  • addTextItem (x3)
  • circuit-server (x1)
  • www-request (x3)

Tags

  • node-red-contrib-circuit
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option