Last active
June 8, 2021 02:46
-
-
Save ruipeterpan/25a0107cd93760c4a9aea4cc7cbe93e1 to your computer and use it in GitHub Desktop.
iOS widget that can be used with Scriptable (https://scriptable.app/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Variables used by Scriptable. | |
// These must be at the very top of the file. Do not edit. | |
// icon-color: yellow; icon-glyph: fire; | |
const goupdate = true; | |
const $ = new importModule("Env"); | |
console.log("Import success!") | |
async function getJson(userid) { | |
/* | |
Gets a json object as the response of an http request. | |
If passed in userid, invoke users.getPresence. | |
Otherwise, invoke users.list. | |
*/ | |
// get a user token from https://api.slack.com/methods/oauth.token | |
var token = "REPLACE_ME_WITH_USER_TOKEN_STRING_THAT_STARTS_WITH_xoxp"; | |
const temp_url = (userid != null) ? | |
("https://slack.com/api/users.getPresence?token="+token+"&user="+userid+"&pretty=1"): | |
("https://slack.com/api/users.list?token="+token+"&pretty=1"); | |
const request = { | |
url: temp_url, | |
}; | |
const res = await $.get(request); | |
return res; | |
} | |
async function getStatus() { | |
/* | |
Invoke getJson() and transform into dict {name: presence} for widget display. | |
The usernames of the members followed need to be edited in the variable following. | |
*/ | |
// transform json into dict {id: name} | |
var id_dict = {}; | |
const people = await getJson(); | |
const members = people["members"]; | |
for (var i in members) { | |
var id = members[i]["id"]; | |
var name = ("real_name" in members[i]) ? | |
(members[i]["real_name"]): | |
(members[i]["name"]); | |
id_dict[id] = name; | |
} | |
// transform dict {id: name} into {name: presence} | |
var isActive = {}; | |
following = ["REPLACE_ME_WITH_USERNAME", "REPLACE_ME_WITH_USERNAME", "REPLACE_ME_WITH_USERNAME", "REPLACE_ME_WITH_USERNAME"]; | |
for (var id in id_dict) { | |
var name = id_dict[id]; | |
if (following.indexOf(name) >= 0) { | |
const status = await getJson(id); | |
var stat = status["presence"]; | |
isActive[name] = stat; | |
} | |
} | |
return isActive; | |
} | |
function createWidget(res) { | |
/* | |
Create a widget in scriptable. | |
*/ | |
const w = new ListWidget(); | |
const bgColor = new LinearGradient(); | |
bgColor.colors = [new Color("#1c1c1c"), new Color("#29323c")]; | |
bgColor.locations = [0.0, 1.0]; | |
w.backgroundGradient = bgColor; | |
w.addSpacer(); | |
w.spacing = 5; | |
var now = new Date(); | |
var hours = (now.getHours()<10?'0':'') + now.getHours(); | |
var minutes = (now.getMinutes()<10?'0':'') + now.getMinutes(); | |
var time = "Last updated: " + hours + ":" + minutes; | |
const time_line = w.addText(time); | |
time_line.font = new Font('SF Compact', 11); | |
time_line.textColor = Color.white(); | |
time_line.textOpacity = 1.0; | |
for (var name in res) { | |
var presence = res[name]; | |
var emoji = (presence == "active") ? ("✅") : ("❌") | |
const line = w.addText(name.concat(": ").concat(emoji)); | |
line.font = new Font('SF Compact', 11); | |
line.textColor = Color.white(); | |
line.textOpacity = 1.0; | |
} | |
w.addSpacer(); | |
w.spacing = 5; | |
w.presentSmall(); | |
return w; | |
} | |
const res = await getStatus(); | |
let widget = createWidget(res); | |
Script.setWidget(widget); | |
Script.complete(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment