-
-
Save tokuzou0829/f6c3f1cc600dd0dfcaa66be744c01cad to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| var scriptProperties = PropertiesService.getScriptProperties(); | |
| var token = scriptProperties.getProperty('Token'); | |
| var cookie = scriptProperties.getProperty('Cookie'); | |
| var workspaceUrl = scriptProperties.getProperty('WorkspaceUrl'); | |
| var spotifyApiProxyUrl = scriptProperties.getProperty('SpotifyApiProxyUrl'); | |
| var emojiName = scriptProperties.getProperty('emojiName'); | |
| var songName = scriptProperties.getProperty('songName'); | |
| function uploadEmoji(fileurl,emojiName) { | |
| console.log(emojiName + ": uploading " + 'test.jpeg'); | |
| var apiUrl = "https://slack.com/api/emoji.add"; | |
| var formData = { | |
| 'mode': 'data', | |
| 'name': emojiName, | |
| 'image': Utilities.newBlob(UrlFetchApp.fetch(fileurl).getBlob().getBytes(), "image/png", 'test.jpeg'), | |
| 'token': token | |
| }; | |
| var options = { | |
| 'method' : 'post', | |
| 'payload' : formData, | |
| 'muteHttpExceptions': true, | |
| 'headers': { | |
| 'Cookie': cookie | |
| } | |
| }; | |
| var response = UrlFetchApp.fetch(apiUrl, options); | |
| var responseCode = response.getResponseCode(); | |
| var responseBody = response.getContentText(); | |
| if (responseCode == 429) { | |
| var retryAfter = parseInt(response.getHeaders()['Retry-After'] || '0'); | |
| console.log("Slack rate limit hit, retrying after " + retryAfter + " seconds"); | |
| Utilities.sleep(retryAfter * 1000); | |
| return uploadEmoji(fileurl,emojiName); | |
| } else if (responseCode != 200) { | |
| throw "HTTP " + responseCode + ": " + responseBody; | |
| } | |
| var apiResponse = JSON.parse(responseBody); | |
| if (!apiResponse.ok) { | |
| throw apiResponse.error; | |
| } | |
| console.log(emojiName + ": upload successful"); | |
| return emojiName; | |
| } | |
| function setStatus(emojiName, songName) { | |
| var profile = { | |
| 'token': token, | |
| 'profile': JSON.stringify({ | |
| 'status_emoji': ':' + emojiName + ':', | |
| 'status_text': 'Playing: ' + songName, | |
| 'status_text_canonical': '', | |
| 'ooo_message': '' | |
| }), | |
| '_x_reason': 'CustomStatusModal:handle_save', | |
| '_x_mode': 'online', | |
| '_x_sonic': 'true', | |
| '_x_app_name': 'client' | |
| }; | |
| var options = { | |
| 'method' : 'post', | |
| 'payload' : profile, | |
| 'headers': { | |
| 'Cookie': cookie | |
| } | |
| }; | |
| UrlFetchApp.fetch(workspaceUrl + "/api/users.profile.set", options); | |
| } | |
| function removeEmoji() { | |
| var formData = { | |
| 'token': token, | |
| 'name': emojiName, | |
| '_x_reason': 'customize-emoji-remove', | |
| '_x_mode': 'online' | |
| }; | |
| var options = { | |
| 'method' : 'post', | |
| 'payload' : formData, | |
| 'headers': { | |
| 'Cookie': cookie | |
| } | |
| }; | |
| UrlFetchApp.fetch(workspaceUrl + "/api/emoji.remove", options); | |
| } | |
| function clearStatus() { | |
| var profile = { | |
| 'profile': JSON.stringify({ | |
| 'status_text': '', | |
| 'status_emoji': '', | |
| 'status_text_canonical': null | |
| }), | |
| 'token': token, | |
| '_x_reason': 'CustomStatusModal:handle_save', | |
| '_x_mode': 'online', | |
| '_x_sonic': 'true', | |
| '_x_app_name': 'client' | |
| }; | |
| var options = { | |
| 'method' : 'post', | |
| 'payload' : profile, | |
| 'headers': { | |
| 'Cookie': cookie | |
| } | |
| }; | |
| UrlFetchApp.fetch(workspaceUrl + "/api/users.profile.set", options); | |
| } | |
| function getSpotifyNowPlaying() { | |
| var response = UrlFetchApp.fetch(spotifyApiProxyUrl); | |
| if (response.getResponseCode() == 204) { | |
| return null; | |
| } | |
| var data = JSON.parse(response.getContentText()); | |
| if (!data.is_playing || data.currently_playing_type == "episode" || data.currently_playing_type == "ad") { | |
| return null; | |
| } | |
| return data; | |
| } | |
| function randomName(n) { | |
| var chars = "abcdefghijklmnopqrstuvwxyz0123456789"; | |
| var result = ''; | |
| for (var i = 0; i < n; i++) { | |
| result += chars.charAt(Math.floor(Math.random() * chars.length)); | |
| } | |
| return result; | |
| } | |
| function loop() { | |
| var r = getSpotifyNowPlaying(); | |
| if (r != null) { | |
| if (songName != r.item.name) { | |
| if (emojiName != '') { | |
| console.log("絵文字をリセット"); | |
| removeEmoji(); | |
| } | |
| console.log("絵文字を保存"); | |
| newemojiName = randomName(20); | |
| scriptProperties.setProperty('emojiName',newemojiName); | |
| scriptProperties.setProperty('songName',r.item.name); | |
| var fileId = r.item.album.images[0].url; | |
| console.log("絵文字をアップロード"); | |
| var e = uploadEmoji(fileId, newemojiName); | |
| console.log("絵文字をステータスに設定"); | |
| setStatus(e, r.item.name); | |
| } | |
| } else { | |
| if (emojiName != '') { | |
| console.log("絵文字をリセット"); | |
| removeEmoji(); | |
| console.log("ステータスを削除"); | |
| clearStatus(); | |
| scriptProperties.setProperty('songName',''); | |
| scriptProperties.setProperty('emojiName',''); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment