Skip to content

Instantly share code, notes, and snippets.

@vkgtaro
Last active March 3, 2017 17:15
Show Gist options
  • Save vkgtaro/79d1afe37bcf4cf05696b76c076fdb4e to your computer and use it in GitHub Desktop.
Save vkgtaro/79d1afe37bcf4cf05696b76c076fdb4e to your computer and use it in GitHub Desktop.
お知らせシートを自分なりに書き換えて chatwork 対応してみた
/**
* お知らせシートの messages シートから、お知らせを生成して chatwork に投稿する
*/
var Notifier = function () {
this.config_sheet_name = 'configuration';
this.messages_sheet_name = 'messages';
this.rooms_sheet_name = 'rooms';
this.default_delivery_hour = 10;
this.default_delivery_minute = 0;
this.configuration = {};
this.rooms = {};
}
Notifier.prototype.renew_rooms_sheet = function (rooms) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(this.rooms_sheet_name);
sheet.deleteRows(2, sheet.getLastRow());
for ( var i in rooms ) {
if ( rooms[i]["type"] != "group" ) { continue; }
sheet.appendRow([rooms[i]["name"], rooms[i]["room_id"]]);
}
}
Notifier.prototype.get_rows_from_sheet = function (sheet_name, start_row, start_column) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet_name);
return sheet.getRange(start_row, start_column, sheet.getLastRow(), sheet.getLastColumn()).getValues();
}
Notifier.prototype.get_config = function (config_key) {
return this.get_value_from_sheet_with_cache("configuration", "config_sheet_name", config_key, 2);
}
Notifier.prototype.get_messages = function () {
var rows = this.get_rows_from_sheet(this.messages_sheet_name, 2, 1);
// using second line as header line.
var headers = rows.shift();
var today = new Date();
var messages = {};
for (var a in rows) {
// ignore if null line.
if ( ! rows[a][0] ) {
continue;
}
var line = this.map_header_to_key(headers, rows[a]);
line['hour'] = this.get_default_unless_expected_type("number", line['hour'], this.default_delivery_hour);
line['minute'] = this.get_default_unless_expected_type("number", line['minute'], this.default_delivery_minute);
// ignore if is not target.
if ( this.is_target_line(line, today) === false ) {
continue;
}
if ( (line['room'] in messages) === false) {
messages[line['room']] = [];
}
var properties = {'name': line['name'], 'title': line['title'], 'text': line['text']};
messages[line['room']].push(properties);
}
return messages;
}
Notifier.prototype.is_target_line = function (line, today) {
var today_str = this.format_date_to_yyyymmdd(today);
var start_str = this.format_date_to_yyyymmdd(line['start']);
var end_str = this.format_date_to_yyyymmdd(line['end']);
// ignore if expired.
if ( (start_str && start_str > today_str) || (end_str && today_str > end_str) ) {
return false;
}
// ignore if is not target day
if ( ! this.is_target_day(line['interval'], today) ) {
return false;
}
// ignore if is not target time
if (today.getHours() != line['hour'] || today.getMinutes() != line['minute']) {
return false;
}
return true;
}
Notifier.prototype.get_default_unless_expected_type = function (type, target_value, default_value) {
if (typeof target_value === type) {
return target_value;
}
return default_value;
}
Notifier.prototype.format_date_to_yyyymmdd = function (date) {
if (date instanceof Date) {
return Utilities.formatDate(date, "JST", "yyyy/MM/dd");
}
// this function does nothing if it is not date object.
return date;
}
Notifier.prototype.map_header_to_key = function (header, row) {
var hash = {};
for ( var a in header ) {
hash[header[a]] = row[a];
}
return hash;
}
Notifier.prototype.is_target_day = function (interval, today) {
var day = today.getDay(); // weekday
var day_of_week = ["日曜", "月曜", "火曜", "水曜", "木曜", "金曜", "土曜"];
if (interval === "毎日") {
return true;
}
// 指定曜日か
if ( day_of_week[day] === interval) {
return true;
}
if (interval == "平日" && 0 < day && day < 6) {
return true;
}
return false;
}
Notifier.prototype.get_room_id_by_name = function (room_name) {
return this.get_value_from_sheet_with_cache("rooms", "rooms_sheet_name", room_name, 2);
}
// get value by key from sheet if not have cache.
Notifier.prototype.get_value_from_sheet_with_cache = function (property_name, sheet_name, key, start_row) {
if ( this[property_name][key] ) {
return this[property_name][key];
}
var rows = this.get_rows_from_sheet(this[sheet_name], start_row, 1);
for (var a in rows) {
if ( rows[a][0] === "" ) { break; }
this[property_name][rows[a][0]] = rows[a][1];
}
return this[property_name][key];
}
Notifier.prototype.notify = function (messages) {
for (var room_name in messages) {
var room_id = this.get_room_id_by_name(room_name);
var message = "";
var properties = messages[room_name];
for (var i in properties) {
var property = properties[i];
if (property['title']) {
message += "[info][title]"+property['title']+"[/title]"+property['text']+"[/info]\n";
}
else {
message += property['text']+"\n";
}
}
this.post_to_chatwork(message, room_id, this.get_config('api_token'), this.get_config('message_api_url'));
}
}
Notifier.prototype.post_to_chatwork = function (message, room_id, api_token, api_url) {
api_url = api_url.replace(/\{room_id\}/, room_id);
var payload = {
"body": message
};
var options = {
"headers": {"X-ChatWorkToken": api_token},
"method" : "post",
"payload" : payload
};
Logger.log(api_url);
Logger.log(options);
UrlFetchApp.fetch(api_url, options);
}
Notifier.prototype.get_chatwork_rooms = function (api_token, api_url) {
var options = {
"headers": {"X-ChatWorkToken": api_token},
"method" : "get",
};
// I don't know when chatwork is down ;-p
var res = UrlFetchApp.fetch(api_url, options).getContentText();
return JSON.parse(res);
}
// main! it's trigger!
function main() {
var notifier = new Notifier();
var messages = notifier.get_messages();
notifier.notify(messages);
}
// renew the room sheet on start spreadsheet. it's triiger.
function get_rooms_to_sheet() {
var notifier = new Notifier();
var rooms = notifier.get_chatwork_rooms(notifier.get_config("api_token"), notifier.get_config("rooms_api_url"));
notifier.renew_rooms_sheet(rooms);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment