In this tutorial, we will be going over how to make a button module toggle your presence on Slack from auto to away. Auto makes you active if your actively on Slack while away will make you away in any scenario.
- Wia Dot One Buy yours here
- Wia Button Module Buy yours here
- Micro USB Cable Buy yours here
- Battery Power Pack Buy yours here
- Account on Wia
- Account at Wia You will need to be registered and /or logged in to your Wia account at https://www.wia.io/.
- Set up your Dot One You will need to set up your Dot One and you can find the tutorial on how to do that here.
- Set up your code project Now you will need to create a space and then a code project on your Wia Dashboard
- Note: This portion requires a decent background in coding, specifically in C++ in order to understand how the modules communicate
- Below is the code for this project which you can follow by reading the comments attached to the side of the code.
- Feel free to try and rewrite this code yourself so you can better understand the device! Some advice if you are having trouble is to make a temporary Block Project and take note of what code is pasted into the preview window when you drag certain blocks over!
/* The following instructions and comments are based on the assumption that you are familiar with the syntax of C++ and a few functions and tools that are commonly used in C++ and classes used with Wia.*
///These are libraries stored by Wia which are necessary for Wia products
#include <WiFi.h>
#include <Wia.h>const int button = 17; /* This is the pinout for the button
module*/
int status = 0; // This is the status that is toggled through
Wia wiaClient = Wia(); /* This is specific to Wia and necessary for Wia to
function*/
void setup()
{
WiFi.begin(); // this starts the connection
delay(2500); // delay is necessary for the WIFI to work properly
pinMode(button, INPUT_PULLDOWN); // this sets up the pin for the button
}
void loop()
{
while (digitalRead(button) == HIGH) /*this keeps looping through while the
button is pressed*/
{
if (status == 0)
{
/* when the status was on away and the button is pressed toggle
to auto and switch status to 1*/
wiaClient.createEvent("toggle-auto"); /* this is the event
which initiates the flow to toggle the slack presence with an
HTTP request (refer to flows code)*/
status = 1; // status 1 represents auto
while (digitalRead(button) == HIGH)
{ // this loop ensures that one click toggles once
delay(10); /* keep delaying for 10ms until the button is
released
}
}
else if (status == 1) /* when the status was on auto and the
button is pressed toggle to away and switch status to 0*/
{
wiaClient.createEvent("toggle-away"); /* this is the event
which initiates the flow to toggle the slack presence with an
HTTP request (refer to flows code)*/
status = 0; // status 0 represents away
while (digitalRead(button) == HIGH)
{
delay(10); /* keep delaying for 10ms until the button is
released*/
}
}
}
}
If you are having trouble reading this code go to the following link for a better view:
https://community.wia.io/d/80-slack-toggle-presence-with-wia-button-module-and-dot-one
Connecting the Button Module to the Dot One- To start off by plugging in your button module into the Dot One like how it is done in Figure 2.
Now we can start setting up the flows to allow the events that the button creates to communicate with a Slack API which changes your presence on Slack.
- To start off you will need a Slack account which you can make Here
- Once you have made a Slack account , you have a workspace and starting channels go to this link to get a legacy token.
- Be very careful with this token because this token can give anyone essentially full access to your Slack account and which could be a large breach of your security on Slack. Treat a legacy token as if it were the password to your account.
- Once you have this token go to the slack API user.setPresence page and if you go to the tester tab you can see where you can test this HTTP request through the Slack API website.
- Now you have the HTML request needed to make the button function. This request can be found as a URL in pink on this page just below where it says URL. Copy this URL and go back to the Wia dashboard and go to the flows tab and make a new flow.
- Within your new flow we are going to create two events one of them for the toggle-away event that you can see in the code above and one for the toggle-auto event which you can also see in the code above. The settings of these two events are in Figure 4 and Figure 5 below.
- Now you need to create a function that based on which event is triggered the Slack status will change. To do this we need to drag a run function into the work space. Run Function is found in the Logic tab under Wia. Here is the code which you need to put in the function.
var event = input.body.name;
if (event == "toggle-away" )
{
output.body = "slack.com/api/users.setPresence?token=xoxp-000000000000-000
000000000-000000000000-000000000000000000000000&presence=away&pretty=1";
/* The link above is the link you copied from the Slack API (without the
https://) test although you need to make sure that at the end of the HTTP
request it says "presence= away" because this will make the presence set
to away*/
}
else
{
output.body = "slack.com/api/users.setPresence?token=xoxp-000000000000-000
000000000-000000000000-000000000000000000000000&presence=auto&pretty=1";";
/* The link above is the link you copied from the Slack API test (without
the https://) although you need to make sure that at the end of the HTTP
request it says "presence= auto" because this will make the presence set
to auto*/
}
If you are having trouble reading this code go to the following link for a better view:
https://community.wia.io/d/80-slack-toggle-presence-with-wia-button-module-and-dot-one
- Now that you have made the run function you need to drag an HTTP Services node into the workspace. These can be found in the Services tab under Wia.
- In the settings for this node you should copy the settings from Figure 6.
Figure 6
- As you can see in this figure we have our input coming from the output of the run function which outputs the HTTP request without the https://, thus we add https:// to the front of the URL that this will post to. Then click update, turn on the node, connect the nodes as seen in figure 7, make sure you code is deployed to your Dot One and your ready to go!
- The whole flow connected is shown below in Figure 7.
Figure 7
Comments