Skip to content

Instantly share code, notes, and snippets.

@senseisimple
Last active April 15, 2024 05:29
Show Gist options
  • Select an option

  • Save senseisimple/19bcffdf072abbe65c0a29072b1faa0e to your computer and use it in GitHub Desktop.

Select an option

Save senseisimple/19bcffdf072abbe65c0a29072b1faa0e to your computer and use it in GitHub Desktop.
MS Teams status keeper - presence API call
// ==UserScript==
// @name Teams Presence Keeper
// @namespace Teams Presence Keeper
// @description Teams Presence Keeper
// @version 2.1
// @grant none
// @match *://*.teams.microsoft.com/*
// @match *://teams.microsoft.com/*
// @noframes
// @run-at document-idle
// @inject-into auto
// @source https://gist.github.com/senseisimple/19bcffdf072abbe65c0a29072b1faa0e
// @updateURL https://gist.github.com/senseisimple/19bcffdf072abbe65c0a29072b1faa0e/raw/497304c8c4c8f1bf6a0ec9fe680c4d0c7575cdd2/teams_keep_status.user.js
// @downloadURL https://gist.github.com/senseisimple/19bcffdf072abbe65c0a29072b1faa0e/raw/497304c8c4c8f1bf6a0ec9fe680c4d0c7575cdd2/teams_keep_status.user.js
// ==/UserScript==
//CONFIGURATION:
/**
* Set your preferred status here.
* Possible choices are:
* Available
* AvailableIdle
* Away
* BeRightBack
* Busy
* BusyIdle
* DoNotDisturb
* Offline
* PresenceUnknown
*/
const forcestatus="Available"
/**
* How often to push the status. An interval of around 45 seconds is usually good enough
* this request is supposed to look as if coming from manually setting status within the
* teams interface so avoid setting it to update too frequently.
*/
const force_status_interval=120 //Seconds
const force_active_interval=30
//FUNCTIONS
//Only start with the innermost frame, (iframe#experience-container...) to prevent running multiple times
//if (window.top == window.self) {
// return;
//}
function read_bearer() {
for (const i in localStorage) {
if (
i.startsWith("ts.") &&
i.endsWith("cache.token.https://presence.teams.microsoft.com/")
) {
return JSON.parse(localStorage[i]).token;
}
}
return false;
}
function put_forced_status(status) {
let statii=["Available", "AvailableIdle", "Away", "BeRightBack", "Busy", "BusyIdle", "DoNotDisturb", "Offline", "PresenceUnknown"]
if (! statii.includes(forcestatus)) {
console.log("Invalid status string configured: "+forcestatus);
return false;
}
var token = read_bearer();
if (token === false) {
console.log("No session auth token found. Are you logged into teams?")
return false;
}
fetch("https://presence.teams.microsoft.com/v1/me/forceavailability/", {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: `{"availability":"${status}"}`,
method: "PUT",
}).then(
(response) => console.log(`forcestatus (availability) ${forcestatus} - OK`),
(response) => console.log(`forcestatus (availability) ${forcestatus} - ERROR`)
);
}
function put_forced_active(status) {
let statii=["Available", "AvailableIdle", "Away", "BeRightBack", "Busy", "BusyIdle", "DoNotDisturb", "Offline", "PresenceUnknown"]
if (! statii.includes(forcestatus)) {
console.log("Invalid status string configured: "+forcestatus);
return false;
}
var token = read_bearer();
if (token === false) {
console.log("No session auth token found. Are you logged into teams?")
return false;
}
fetch("https://presence.teams.microsoft.com/v1/me/endpoints/", {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: `{"id":"60d8d816-7a65-4bde-90c7-70cc0d0e9b60","activity":"${status}","availability":"${status}","deviceType":"Web"}`,
method: "PUT",
}).then(
(response) => console.log(`forcestatus (endpoint) ${forcestatus} - OK`),
(response) => console.log(`forcestatus (endpoint) ${forcestatus} - ERROR`)
);
fetch("https://presence.teams.microsoft.com/v1/me/reportmyactivity/", {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: `{"endpointId":"60d8d816-7a65-4bde-90c7-70cc0d0e9b60","isActive":true}`,
method: "POST",
}).then(
(response) => console.log(`forcestatus (reportactive) ${forcestatus} - OK`),
(response) => console.log(`forcestatus (reportactive) ${forcestatus} - ERROR`)
);
return true;
}
//var meta = document.createElement("meta");
document.querySelector("[name='theme-color']").content = "#1f1f3d";
//MAIN LOOP, ensure we don't get any overlapping/backlogged/queued timers
console.log("Started Teams forcestatus",forcestatus,force_status_interval);
(function loop() {
setTimeout(() => {
put_forced_status(forcestatus);
loop();
}, force_status_interval * 1000);
})();
console.log("Started Teams forceactive",forcestatus,force_active_interval);
(function loop() {
setTimeout(() => {
put_forced_active(forcestatus);
loop();
}, force_active_interval * 1000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment