Created
November 4, 2021 13:17
-
-
Save mogya/c3e2b4654d4898c7a06d300eb6443064 to your computer and use it in GitHub Desktop.
Google Spread Sheet Twitter bot
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
function tweetTodaysData(){ | |
const today = new Date(); | |
const range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("data").getDataRange(); | |
for (let r = 1; r < range.getNumRows()+1; r++) { | |
const date = range.getCell(r, 1).getValue(); | |
if (typeof date.getMonth !== 'function') { continue; } | |
if (date.getMonth() == today.getMonth() && date.getDate() == today.getDate()){ | |
tweetText(range.getCell(r, 6).getValue()); | |
} | |
} | |
} | |
function prepareTweetRow() { | |
const dataSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("data"); | |
const tweetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("tweet"); | |
const activeCell = dataSheet.getCurrentCell(); | |
const activeRow = dataSheet.getRange(activeCell.getRow(), 1, 1, dataSheet.getMaxColumns()); | |
const targetRow = tweetSheet.getRange(2, 1, 1, dataSheet.getMaxColumns()); | |
activeRow.copyTo(targetRow); | |
targetRow.activate(); | |
} | |
function tweet() { | |
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("tweet"); | |
sheet.activate(); | |
let currentTarget = sheet.getRange('F2'); | |
while(true){ | |
const status = currentTarget.getValue(); | |
if (status === "") { break; } | |
// console.log("will tweet "+status); | |
tweetText(status); | |
currentTarget = sheet.getRange(currentTarget.getRow()+1, currentTarget.getColumn()); | |
// console.log(`currentTarget ${currentTarget.getRow()},${currentTarget.getColumn()}`); | |
} | |
} | |
function tweetText(status){ | |
const service = twitterApi().getService(); | |
const response = service.fetch('https://api.twitter.com/1.1/statuses/update.json', { | |
method: 'post', | |
payload: { status: status } | |
}); | |
} | |
function twitterApiParameters(){ | |
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); | |
const apiKey = "*******"; | |
const apiSecret = "*******"; | |
return {key: apiKey, secret: apiSecret} | |
} | |
let myTwitterApiObj = null ; | |
function twitterApi(){ | |
if (!myTwitterApiObj){ | |
const params = twitterApiParameters(); | |
// console.log(params) | |
myTwitterApiObj = TwitterWebService.getInstance(params.key, params.secret); | |
} | |
return myTwitterApiObj; | |
} | |
// 認証 | |
function authTwitterApi(){ | |
twitterApi().authorize(); | |
} | |
// 認証解除 | |
function resetTwitterApiAuth() { | |
twitterApi().reset(); | |
} | |
// 認証後のコールバック | |
function authCallback(request) { | |
return twitterApi().authCallback(request); | |
} | |
function onOpen() { | |
const ui = SpreadsheetApp.getUi(); | |
const menu = ui.createMenu("ツイート"); | |
menu.addItem('ツイート', 'tweetTodaysData'); | |
// メニューからやるとログが見れなくて辛いので、スクリプトエディタ上から直接実行したほうが良い | |
// menu.addItem('認証', 'authTwitterApi'); | |
// menu.addItem('認証解除', 'resetTwitterApiAuth'); | |
menu.addToUi(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment