Last active
June 6, 2020 06:48
-
-
Save hidao80/6cce0e161086d52a4f51c2939acaf315 to your computer and use it in GitHub Desktop.
【Mastodon】実行時、メンションされていたらDMを返す
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
/** | |
* リアクションtootスクリプト | |
* | |
* メンションを発見したら直ちにリプライ | |
* | |
* アクセストークンとインスタンスのAPI URLはプロジェクトのプロパティに設定しておく。 | |
* ソースコードとトークンおよびインスタンスを切り離すことにより、スクリプトの再利用性が高まった。 | |
* | |
* リアクションtootスクリプト © @hidao80 クリエイティブ・コモンズ・ライセンス(表示4.0 国際)https://creativecommons.org/licenses/by/4.0/ | |
*/ | |
/** | |
* ランダムに変身するメッセージを返す | |
* | |
* @return string msg - 返信するメッセージ文字列 | |
*/ | |
function say() { | |
const msg = [ | |
"レ、レイン!" | |
,"おのれ東方不敗!" | |
,"ヒート、エンドゥ!" | |
,"見よ、東方は赤く燃えている!" | |
,"天然自然!" | |
,"明鏡止水に至るにはどうすれば…" | |
]; | |
return msg[Math.floor(Math.random()*6)]; | |
} | |
/** | |
* mastodonに投稿する | |
* | |
* @param string message 投稿内容 | |
* @param string visibility 投稿スコープ | |
* @param string token Mastodon API トークン | |
* @param string url Mastodon RESTful API URL(statuses) | |
* @param string tootId メンションされたtootのID | |
*/ | |
function postUpdateToots(message,visibility,token,url,tootId){ | |
// API オプションの作成 | |
const options = | |
{ | |
"method" : "post", | |
"payload" : "status=" + message + "&visibility=" + visibility + "&in_reply_to_id=" + tootId, | |
"headers" : {"Authorization" : "Bearer "+ token} | |
}; | |
UrlFetchApp.fetch(url,options); | |
} | |
/** | |
* Mastoadonのアカウントにメッセージを投稿する | |
* | |
* @param string message 投稿内容 | |
* @param string tootId メンションしてきたtootのID | |
*/ | |
function sendMessage(message, tootId){ | |
// プロジェクトのプロパティからbotの固有値を取得 | |
const token = PropertiesService.getScriptProperties().getProperty("token"); | |
const url = PropertiesService.getScriptProperties().getProperty("url"); | |
// visibility Either "direct", "private", "unlisted" or "public" | |
const visibility = "direct"; | |
postUpdateToots(message,visibility,token,url,tootId); | |
} | |
/** | |
* メンションされたtootのリストを返す | |
* | |
* @return array メンションしたユーザ名とtootIDの組の配列 | |
*/ | |
function getMentions() { | |
var mentionToots = []; | |
// プロジェクトのプロパティからbotの固有値を取得 | |
const token = PropertiesService.getScriptProperties().getProperty("token"); | |
const url = PropertiesService.getScriptProperties().getProperty("notifications_url"); | |
var lastId = PropertiesService.getScriptProperties().getProperty("lastId"); | |
// API オプションの作成 | |
const options = | |
{ | |
"headers" : {"Authorization" : "Bearer "+ token} | |
}; | |
// 取得したホームタイムラインjson(15件分)を文字列からオブジェクトに変換 | |
Logger.log(lastId); | |
const json_str = UrlFetchApp.fetch(url + "?since_id=" + lastId,options); | |
const json = JSON.parse(json_str); | |
// 取得したjsonからメンションtootを抜き出す(15件のうち) | |
json.forEach( function (notification) { | |
// Logger.log(notification.account); | |
if (notification.type === "mention") { | |
Logger.log(notification.account.acct); | |
mentionToots.push({tootId: notification.status.id, acct: notification.account.acct}); | |
} | |
if (notification.id > lastId) lastId = notification.id; | |
}); | |
PropertiesService.getScriptProperties().setProperty("lastId", lastId); | |
return mentionToots; | |
} | |
/** | |
* スプレッドシートから投稿内容を取得し、投稿関数を呼び出す | |
* setTrigger関数から呼び出されるようにしておくこと | |
*/ | |
function main() { | |
const mentionToots = getMentions(); | |
// メンションがあれば返信する | |
if (mentionToots !== []) { | |
// メンションしてくれた人に返信 | |
mentionToots.forEach( function (item) { | |
sendMessage("@" + item.acct + " " + say(),item.tootId); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment