diff --git a/workflows/Daily Space Quiz Bot: NASA APOD to Slack with GPT-4 Turbo-10994/daily_space_quiz_bot_nasa_apod_to_slack_with_gpt-4_turbo.json b/workflows/Daily Space Quiz Bot: NASA APOD to Slack with GPT-4 Turbo-10994/daily_space_quiz_bot_nasa_apod_to_slack_with_gpt-4_turbo.json new file mode 100644 index 000000000..7c8bc754a --- /dev/null +++ b/workflows/Daily Space Quiz Bot: NASA APOD to Slack with GPT-4 Turbo-10994/daily_space_quiz_bot_nasa_apod_to_slack_with_gpt-4_turbo.json @@ -0,0 +1 @@ +{"id":"q4dgSdg1llXoZkpP","meta":{"instanceId":"d02f9239324256c2c5ad7338bdc50f666d36f02cbba76692f30d3a0dd5883c18"},"name":"Daily Space Quiz Bot with NASA APOD Integration","tags":[],"nodes":[{"id":"12d15927-8b9c-4d7c-8ec1-e50d54cd8194","name":"Schedule: Daily at 21:00","type":"n8n-nodes-base.scheduleTrigger","position":[0,256],"parameters":{"rule":{"interval":[{"triggerAtHour":21}]}},"typeVersion":1.2},{"id":"b38d3599-15a1-4a5b-8302-4a5e2a84faa2","name":"Workflow Configuration","type":"n8n-nodes-base.set","position":[448,256],"parameters":{"options":{},"assignments":{"assignments":[{"id":"id-1","name":"quizDifficulty","type":"string","value":"beginner"},{"id":"id-2","name":"explanationMaxChar","type":"number","value":400},{"id":"id-3","name":"llmTone","type":"string","value":"friendly and engaging"},{"id":"id-4","name":"channelId","type":"string","value":"YOUR_SLACK_CHANNEL_ID"},{"id":"id-6","name":"answerTimeoutMin","type":"number","value":15}]},"includeOtherFields":true},"typeVersion":3.4},{"id":"6770d3f0-573a-4ea5-9969-617b5cb1b5c9","name":"OpenAI: Create Quiz","type":"@n8n/n8n-nodes-langchain.openAi","position":[672,256],"parameters":{"modelId":{"__rl":true,"mode":"list","value":"gpt-4-turbo"},"options":{"textFormat":{"textOptions":{"type":"json_object"}}},"responses":{"values":[{"content":"=You are a space-savvy quiz master. Based on the NASA Astronomy Picture of the Day information below, create one multiple-choice question with 4 options. Your tone should be {{ $('Workflow Configuration').item.json.llmTone }}. The difficulty should be {{ $('Workflow Configuration').item.json.quizDifficulty }}.\n\n# Information\n- Title: {{ $('Get APOD').item.json.title }}\n- Explanation: {{ $('Get APOD').item.json.explanation }}\n- Image URL: {{ $('Get APOD').item.json.url }}\n\n# Output Format Rules\n- You MUST output a single JSON object in the following format.\n- Do NOT output any text outside of the JSON object (e.g., \"Here is the JSON\").\n- The explanation must be concise, under {{ $('Workflow Configuration').item.json.explanationMaxChar }} characters.\n- You must generate exactly four choices: A, B, C, and D.\n\n{\n \"question\": \"Your quiz question here\",\n \"choices\": {\n \"A\": \"Choice A\",\n \"B\": \"Choice B\",\n \"C\": \"Choice C\",\n \"D\": \"Choice D\"\n },\n \"answer\": \"A, B, C, or D\",\n \"explanation\": \"Explanation of the correct answer here\",\n \"source_url\": \"{{ $('Get APOD').item.json.url }}\"\n}"}]},"builtInTools":{}},"typeVersion":2},{"id":"e4728bec-8faa-4c1c-9010-4174d69925a3","name":"Slack: Post Quiz","type":"n8n-nodes-base.slack","position":[1024,256],"webhookId":"b80737d5-6ad6-4204-a87d-d1119e06c4cd","parameters":{"text":"==šŸš€ Today's Space Quiz ({{ $('Workflow Configuration').item.json.quizDifficulty }}) šŸš€\n\n*Q. {{ $json.output[0].content[0].text.question }}*\n\n1. {{ $json.output[0].content[0].text.choices.A }}\n2. {{ $json.output[0].content[0].text.choices.B }}\n3. {{ $json.output[0].content[0].text.choices.C }}\n4. {{ $json.output[0].content[0].text.choices.D }}\n\nšŸ‘‡ Vote by clicking one of the reactions below! (Closing in ~{{ $('Workflow Configuration').item.json.answerTimeoutMin }} min)\n\nSource Image: {{ $json.output[0].content[0].text.source_url }}","user":{"__rl":true,"mode":"list","value":""},"select":"=channel","channelId":{"__rl":true,"mode":"list","value":"={{ $('Workflow Configuration').item.json.channelId }}"},"otherOptions":{"unfurl_links":true}},"typeVersion":2.3},{"id":"4ab34bac-7ab3-4604-8c69-20b8b438918b","name":"Wait for Answers","type":"n8n-nodes-base.wait","position":[2144,256],"webhookId":"340a75bf-ae39-4886-aee8-48d34f435f0c","parameters":{"unit":"minutes","amount":"={{ $('Workflow Configuration').item.json.answerTimeoutMin }}"},"typeVersion":1.1},{"id":"60793327-1e16-45d7-9915-dcc0eaf03310","name":"Slack: Get Reactions","type":"n8n-nodes-base.slack","position":[2368,256],"webhookId":"55f484aa-41e4-47c3-a2aa-b5367e4340ba","parameters":{"resource":"reaction","channelId":{"__rl":true,"mode":"id","value":"={{ $('Slack: Post Quiz').item.json.channel }}"},"operation":"get","timestamp":"={{ $('Slack: Post Quiz').item.json.message.ts }}"},"typeVersion":2.3},{"id":"5b4e5f6b-155e-495a-9adf-91836572d4e8","name":"Code: Tally Correct Answers","type":"n8n-nodes-base.code","position":[2592,256],"parameters":{"jsCode":"// Get the correct answer data from the LLM node\nconst quizData = $('OpenAI: Create Quiz').item.json.output[0].content[0].text;\nconst correctAnswerLabel = quizData.answer; // e.g., \"B\"\n\n// Get the reaction data from Slack\nconst reactions = $json.message?.reactions || [];\n\n// Get the Bot's user ID to exclude it from the results\nconst botUserId = $('Slack: Post Quiz').item.json.message.user;\n\n// Map Slack emoji names to the quiz choices\nconst emojiMap = {\n 'one': 'A',\n 'two': 'B',\n 'three': 'C',\n 'four': 'D'\n};\n\n// Tally the IDs of users who reacted with the correct answer\nconst correctUsers = [];\nfor (const reaction of reactions) {\n const answerLabel = emojiMap[reaction.name];\n \n if (answerLabel === correctAnswerLabel) {\n // Filter out the bot's own reaction from the list of users\n const actualUsers = reaction.users.filter(userId => userId !== botUserId);\n \n // Add the filtered list of users to the final correct users array\n correctUsers.push(...actualUsers);\n }\n}\n\n// To get an accurate count of total human participants, we can create a set of unique user IDs.\nconst allUserIds = new Set();\nfor (const reaction of reactions) {\n reaction.users.forEach(userId => {\n if (userId !== botUserId) {\n allUserIds.add(userId);\n }\n });\n}\n\n// Return the results for the next node\nreturn {\n json: {\n quizData: quizData,\n correctAnswer: correctAnswerLabel,\n correctUsers: [...new Set(correctUsers)], // Ensure unique users\n correctCount: [...new Set(correctUsers)].length,\n totalParticipants: allUserIds.size\n }\n};"},"typeVersion":2},{"id":"e83fafc9-3b79-44af-8c5e-852110f084ca","name":"Slack: Post Results","type":"n8n-nodes-base.slack","position":[2816,256],"webhookId":"2b685c49-c626-4b2e-bf1c-e627ea12b45d","parameters":{"text":"=ā° Time's up!\n\nāœ… *Correct Answer: {{ $json.correctAnswer }}. {{ $json.quizData.choices[$json.correctAnswer] }}*\n\n_{{ $json.quizData.explanation }}_\n\n---\n\nšŸ† *Winners ({{ $json.correctCount }}/{{$json.totalParticipants}} correct):*\n{{ $json.correctUsers.length > 0 ? $json.correctUsers.map(u => '<@' + u + '>').join(' ') : 'No one got it right this time!' }}\n\nšŸ“ø [View the original image]({{ $json.quizData.source_url }})","select":"channel","channelId":{"__rl":true,"mode":"name","value":"={{ $('Slack: Post Quiz').item.json.channel }}"},"otherOptions":{}},"typeVersion":2.3},{"id":"c47f9120-bef4-4277-b6ce-68c97eb45c2c","name":"Slack: Add Reaction 1","type":"n8n-nodes-base.slack","position":[1248,256],"webhookId":"0a4fcde5-10de-496a-8888-25f64babc659","parameters":{"name":"one","resource":"reaction","channelId":{"__rl":true,"mode":"list","value":"={{ $('Workflow Configuration').item.json.channelId }}"},"timestamp":"={{ $json.message.ts }}"},"typeVersion":2.3},{"id":"8f94191c-1908-4cde-ace7-b1c91b3dff90","name":"Slack: Add Reaction 2","type":"n8n-nodes-base.slack","position":[1472,256],"webhookId":"678cb7aa-3a2a-4c0b-ac51-ae41036df0ef","parameters":{"name":"two","resource":"reaction","channelId":{"__rl":true,"mode":"list","value":"={{ $('Workflow Configuration').item.json.channelId }}"},"timestamp":"={{ $('Slack: Post Quiz').item.json.message.ts }}"},"typeVersion":2.3},{"id":"b3a9b131-d744-4c16-a13c-7a89db1bb0aa","name":"Slack: Add Reaction 3","type":"n8n-nodes-base.slack","position":[1696,256],"webhookId":"a59e84ef-84f9-4cef-aaf3-58e2444f0d93","parameters":{"name":"three","resource":"reaction","channelId":{"__rl":true,"mode":"list","value":"={{ $('Workflow Configuration').item.json.channelId }}"},"timestamp":"={{ $('Slack: Post Quiz').item.json.message.ts }}"},"typeVersion":2.3},{"id":"6eb5bff2-f0bc-456d-b61a-eebc6d800e9c","name":"Slack: Add Reaction 4","type":"n8n-nodes-base.slack","position":[1920,256],"webhookId":"27aa0095-4534-477c-ac5c-a6ee8f3d4408","parameters":{"name":"four","resource":"reaction","channelId":{"__rl":true,"mode":"list","value":"={{ $('Workflow Configuration').item.json.channelId }}"},"timestamp":"={{ $('Slack: Post Quiz').item.json.message.ts }}"},"typeVersion":2.3},{"id":"5909d2ff-ebc5-4cbb-9f3b-07706e499323","name":"Get APOD","type":"n8n-nodes-base.nasa","position":[224,256],"parameters":{"additionalFields":{}},"typeVersion":1},{"id":"2cb69bd2-65fa-479d-9548-12cf2ff3c0a4","name":"Sticky Note","type":"n8n-nodes-base.stickyNote","position":[-432,-256],"parameters":{"width":368,"height":640,"content":"## Daily Space Quiz Bot\nThis workflow creates a daily space-themed quiz in Slack.\n\n### How it Works\n1. **Triggers Daily:** Runs automatically at a set time.\n2. **Fetches NASA's Picture of the Day (APOD):** Gets the latest space image and info from NASA.\n3. **Generates Quiz with AI:** Uses an LLM (like OpenAI) to create a multiple-choice question based on the NASA data.\n4. **Posts to Slack:** The quiz is posted in a Slack channel, and the bot adds reactions (1-4) for voting.\n5. **Tallies Results:** After a waiting period, it counts the votes and identifies the winners.\n6. **Announces Winner:** Posts a follow-up message with the correct answer, an explanation, and mentions the winners.\n\n### Setup\n1. **Credentials:** Add your API credentials for NASA, OpenAI, and Slack in the respective nodes.\n2. **Configuration:** In the `Workflow Configuration` node, set your Slack Channel ID and other preferences.\n3. **Activate:** Turn on the workflow to start the daily quizzes!\n"},"typeVersion":1},{"id":"71dd09c5-1c02-4167-b9a4-ae5aa3b3bc95","name":"Sticky Note1","type":"n8n-nodes-base.stickyNote","position":[336,48],"parameters":{"color":7,"width":272,"height":192,"content":"## Workflow Configuration\n\nSet the Slack Channel ID where the quiz will be posted, the quiz difficulty, and other main variables for the workflow here."},"typeVersion":1},{"id":"ce2b5946-8056-4715-a479-62b1b91c0315","name":"Sticky Note2","type":"n8n-nodes-base.stickyNote","position":[1168,64],"parameters":{"color":7,"width":272,"content":"## Add Voting Buttons\n\nThe bot automatically adds reactions (1ļøāƒ£, 2ļøāƒ£, 3ļøāƒ£, 4ļøāƒ£) to the quiz message to act as voting buttons for the users."},"typeVersion":1},{"id":"fedba601-a7c7-42a2-8167-f7bae286e33d","name":"Sticky Note3","type":"n8n-nodes-base.stickyNote","position":[2480,80],"parameters":{"color":7,"width":304,"content":"## Tally Correct Answers\n\nThis Code node tallies the correct answers based on the reactions from users. It's designed to exclude the bot's own reactions from the final count."},"typeVersion":1},{"id":"abecc729-5b75-48a8-9632-2935a7b35fc5","name":"Sticky Note4","type":"n8n-nodes-base.stickyNote","position":[640,48],"parameters":{"color":7,"width":272,"height":176,"content":"## AI Quiz Generation\n\nThis is the prompt used to ask OpenAI (GPT) to generate the quiz. You can customize the tone, output format, language, or even change the AI model used."},"typeVersion":1}],"active":false,"pinData":{},"settings":{"executionOrder":"v1"},"versionId":"53b1f9ff-6a3a-4d4b-8e27-7ae700ddb4a8","connections":{"Get APOD":{"main":[[{"node":"Workflow Configuration","type":"main","index":0}]]},"Slack: Post Quiz":{"main":[[{"node":"Slack: Add Reaction 1","type":"main","index":0}]]},"Wait for Answers":{"main":[[{"node":"Slack: Get Reactions","type":"main","index":0}]]},"OpenAI: Create Quiz":{"main":[[{"node":"Slack: Post Quiz","type":"main","index":0}]]},"Slack: Get Reactions":{"main":[[{"node":"Code: Tally Correct Answers","type":"main","index":0}]]},"Slack: Add Reaction 1":{"main":[[{"node":"Slack: Add Reaction 2","type":"main","index":0}]]},"Slack: Add Reaction 2":{"main":[[{"node":"Slack: Add Reaction 3","type":"main","index":0}]]},"Slack: Add Reaction 3":{"main":[[{"node":"Slack: Add Reaction 4","type":"main","index":0}]]},"Slack: Add Reaction 4":{"main":[[{"node":"Wait for Answers","type":"main","index":0}]]},"Workflow Configuration":{"main":[[{"node":"OpenAI: Create Quiz","type":"main","index":0}]]},"Schedule: Daily at 21:00":{"main":[[{"node":"Get APOD","type":"main","index":0}]]},"Code: Tally Correct Answers":{"main":[[{"node":"Slack: Post Results","type":"main","index":0}]]}}} \ No newline at end of file