i need the code for a custom stream elements widget that lets me keep track of tasks for…
- User
i need the code for a custom stream elements widget that lets me keep track of tasks for the audeience on twitch
- Assistant
Creating a custom Stream Elements widget requires some knowledge of web development and working with Stream Elements API. Here's an example of how you can create a custom task tracker widget:
Step 1: Setting up the project
- Create a new folder for your project.
- Open a code editor like Visual Studio Code and navigate to your project folder.
- Open a terminal within your code editor or navigate to your project folder in a terminal.
- Initialize a new project by running the following command:
npm init -y. - Install necessary dependencies by running the following command:
npm install express axios.
Step 2: Creating the widget code
- Create an
index.jsfile in your project folder. - Open the
index.jsfile and add the following code:
const express = require('express'); const axios = require('axios'); const app = express(); const PORT = 3000; // Change this according to your needs // Stream Elements API endpoint for updating text widgets const streamElementsApiUrl = 'https://api.streamelements.com/kappa/v2/points/HASH_POINT_STORED'; // Store tasks in memory let tasks = []; // Update the Stream Elements widget with the current tasks const updateWidget = async () => { const options = { headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_STREAM_ELEMENTS_API_TOKEN' } }; try { await axios.put(streamElementsApiUrl, { text: tasks.join('\n') }, options); } catch (error) { console.error('Error updating task widget:', error); } }; // API route to add a new task app.post('/task', express.json(), (req, res) => { const { task } = req.body; tasks.push(task); updateWidget(); res.sendStatus(200); }); // API route to remove a completed task app.delete('/task/:index', (req, res) => { const { index } = req.params; if (index >= 0 && index < tasks.length) { tasks.splice(index, 1); updateWidget(); res.sendStatus(200); } else { res.sendStatus(404); } }); // Start the server app.listen(PORT, () => { console.log(`Server running on http://localhost:${PORT}`); });Make sure to replace
HASH_POINT_STOREDwith the specific widget hash provided by Stream Elements andYOUR_STREAM_ELEMENTS_API_TOKENwith your generated Stream Elements API token, both of which you can obtain from the Stream Elements website.Step 3: Running the widget
- Save the changes made to
index.js. - In the terminal, navigate to your project folder.
- Run the command
node index.js. - Your custom task tracker widget should now be available at
http://localhost:3000.
You can integrate and style this widget on your Twitch channel by using browser source in streaming software like OBS or Xsplit, pointing to the URL
http://localhost:3000. You may need to host it on a live server to make it accessible to your audience.Please note that this is a basic example, and you can further enhance the widget based on your requirements and desired functionality.