Smartthings https post request to switchbot api

I have being trying to use my glitch code to make an api post call to switchbot api but is having issues making the post call. The const request = require(‘request’); is not returning the error "cannot find module ‘request’ " does anyone have any suggestions on how to fix this.

‘use strict’;

const express = require(‘express’);
const bodyParser = require(‘body-parser’);
const SmartApp = require(‘@smartthings/smartapp’);

const server = module.exports = express();
server.use(bodyParser.json());

const app = new SmartApp();

const request = require(‘request’);

const bearerToken = ‘‘YOUR_BEARER_TOKEN_HERE’’;
const remoteId = ‘YOUR_REMOTE_ID_HERE’;

/* Only here for Glitch, so that GET doesn’t return an error */
server.get(‘/’, (req, res) => {
res.send(‘Simple SmartApp Example URL: https://’+ req.hostname);
});

/* Handles lifecycle events from SmartThings */
server.post(‘/’, async (req, res) => {
app.handleHttpCallback(req, res);
});

/* Defines the SmartApp */
app.enableEventLogging(2) // Log and pretty-print all lifecycle events and responses
.configureI18n() // Use files from locales directory for configuration page localization
.page(‘mainPage’, (context, page, configData) => {
page.section(‘buttons’, section => {
section.deviceSetting(‘Upbutton’).capabilities([‘button’]).required(true);
});
page.section(‘buttons’, section => {
section.deviceSetting(‘Downbutton’).capabilities([‘button’]).required(true);
});
page.section(‘thermostats’, section => {
section.deviceSetting(‘thermostat’).capabilities([‘thermostatCoolingSetpoint’]).multiple(true).permissions(‘wx’);
});
page.section(‘thermostats1’, section => {
section.deviceSetting(‘thermosta’).capabilities([‘thermostatCoolingSetpoint’]).multiple(true).permissions(‘rx’);
});
})
.updated(async (context, updateData) => {

    await context.api.subscriptions.unsubscribeAll();
    return Promise.all([
       context.api.subscriptions.subscribeToDevices(context.config.Upbutton, 'button', 'button.pushed', 'pushedUpDeviceEventHandler'),
       context.api.subscriptions.subscribeToDevices(context.config.Downbutton, 'button', 'button.pushed', 'pushedDownDeviceEventHandler'),
       context.api.subscriptions.subscribeToDevices(context.config.thermosta, 'thermostatCoolingSetpoint', 'coolingSetpoint', 'tempDeviceEventHandler')
    ])
})
   
  .subscribedEventHandler('tempDeviceEventHandler', (context, deviceEvent) => {
    
   const setpointTemperature = deviceEvent.value;
   console.log('coolingSetpoint haha',setpointTemperature);
    
})
  .subscribedEventHandler('pushedUpDeviceEventHandler', async (context, deviceEvent) => {
    
   const setpointTemperature = await context.api.devices.getCapabilityStatus(context.config.thermosta[0].deviceConfig.deviceId,'main','thermostatCoolingSetpoint');        
    
    console.log('coolingSetpoint haha',setpointTemperature.coolingSetpoint.value);
    // Increment the setpoint temperature by 1 degree
    let newSetpointTemperature = setpointTemperature.coolingSetpoint.value + 1.0;
    
    // Set the new setpoint temperature
 
   /*(async () => {
       const rawResponse = await fetch('https://api.switch-bot.com/v1.0/devices/01-202211271842-92432667/commands', {
        method: 'POST',
        headers: {
       'Accept': 'application/json',
       'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          "command": "setAll",
          "parameter": "26,2,1,on,swing",
          "commandType": "command"})
     });
      const content = await rawResponse.json();

         console.log('POST OUTPUT',content);
       })();*/
const options = {
  method: 'POST',
  url: `https://api.switchbot.me/v2/remotes/${remoteId}/commands`,
  headers: {
  'Content-Type': 'application/json',
  'Authorization': `Bearer ${bearerToken}`
         },
   json: {
 "command": "setAll",
  "parameter": "26,2,1,off,swing",
  "commandType": "command"
 }

};

request(options, function (error, response, body) {
 if (error) throw new Error(error);
 console.log(body);
});
    
   return context.api.devices.sendCommands(context.config.thermostat, 'thermostatCoolingSetpoint', 'setCoolingSetpoint', [newSetpointTemperature]);
})

.subscribedEventHandler('pushedDownDeviceEventHandler', async (context, deviceEvent) => {

    const setpointTemperature =  await context.api.devices.getCapabilityStatus(context.config.thermosta[0].deviceConfig.deviceId,'main','thermostatCoolingSetpoint');
    // Decrement the setpoint temperature by 1 degree
    const newSetpointTemperature = setpointTemperature.coolingSetpoint.value - 1.0;
    
    // Set the new setpoint temperature
    return context.api.devices.sendCommands(context.config.thermostat, 'thermostatCoolingSetpoint', 'setCoolingSetpoint', [newSetpointTemperature]);
   
})

/* Starts the server */
let port = process.env.PORT;
server.listen(port);
console.log(Open: http://127.0.0.1:${port});

Hi and welcome, @mcgowanjj! Try installing the package with npm (open terminal - bottom left corner - and use npm install PACKAGE_NAME)

Thank you for responding I got it to work.

1 Like