Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)

i need the code for a custom stream elements widget that lets me keep track of tasks for…

  1. User

    i need the code for a custom stream elements widget that lets me keep track of tasks for the audeience on twitch

  2. 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

    1. Create a new folder for your project.
    2. Open a code editor like Visual Studio Code and navigate to your project folder.
    3. Open a terminal within your code editor or navigate to your project folder in a terminal.
    4. Initialize a new project by running the following command: npm init -y.
    5. Install necessary dependencies by running the following command: npm install express axios.

    Step 2: Creating the widget code

    1. Create an index.js file in your project folder.
    2. Open the index.js file 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_STORED with the specific widget hash provided by Stream Elements and YOUR_STREAM_ELEMENTS_API_TOKEN with your generated Stream Elements API token, both of which you can obtain from the Stream Elements website.

    Step 3: Running the widget

    1. Save the changes made to index.js.
    2. In the terminal, navigate to your project folder.
    3. Run the command node index.js.
    4. 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.