Skip to content

Instantly share code, notes, and snippets.

@taross-f
Last active January 5, 2017 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taross-f/0d6a45a91f802427f1f492e3b4f092b3 to your computer and use it in GitHub Desktop.
Save taross-f/0d6a45a91f802427f1f492e3b4f092b3 to your computer and use it in GitHub Desktop.
chatwork_bot on GAS
function humidifier() {
if (_todayIsHoliday()) return;
var members = client.get("/rooms/" + roomId + "/members");
var target = _gacha(members);
while (isInvalid(target.account_id)) {
target = _gacha(members);
}
client.sendMessage({room_id: roomId, body: "[To:" + target.account_id + "] " + target.name + "さん\n" +
target.name + "さん(clap)"});
}
function isInvalid(id) {
return invalidIds.indexOf(id) != -1;
}
// 返答処理
function reply() {
var messages = client.get("/rooms/" + roomId + "/messages");
if (!messages) return;
var toMessages = messages.filter(function(e, i, array) {
return e.body.indexOf("[To:" + accountId) != -1
});
var reMessages = messages.filter(function(e, i, array) {
return e.body.indexOf("aid=" + accountId) != -1
});
var responseFunc = function(e, i, array) {
var response = _communicateUserLocalApi(e.body.substring(e.body.indexOf("name") + "name".length), e.account.name);
client.sendMessage(
{
room_id: roomId,
body: "[rp aid=" + e.account.account_id + " to=" + roomId + "-" + e.message_id + "] " +
e.account.name + "さん\n" + "(" + response + ")"
});
};
toMessages.forEach(responseFunc);
reMessages.forEach(responseFunc);
}
function weatherReport() {
if (_todayIsHoliday()) return;
var desc = _getWeather();
client.sendMessage(
{
room_id: roomId,
body: "(" + desc + ")"
});
}
function _communicateDocomoApi(message, name) {
var payload = {
'utt' : message,
'nickname' : name || '',
'place' : '東京'
};
var options = {
'method' : 'POST',
'contentType' : 'application/json',
'payload' : JSON.stringify(payload)
};
var response = UrlFetchApp.fetch("https://api.apigw.smt.docomo.ne.jp/dialogue/v1/dialogue?APIKEY=" + docomoKey, options);
var o = JSON.parse(response.getContentText());
return o.utt;
}
function _communicateUserLocalApi(message, name) {
var options = {
'method' : 'GET',
};
var response = UrlFetchApp.fetch("https://chatbot-api.userlocal.jp/api/chat?key=" + userLocalKey + "&message=" + encodeURI(message.trim()), options);
var o = JSON.parse(response.getContentText());
return o.result;
}
function _getWeather() {
var res = UrlFetchApp.fetch("http://weather.livedoor.com/forecast/webservice/json/v1?city=130010");
var o = JSON.parse(res.getContentText());
Logger.log(o);
return o.description.text;
}
// shuffle util
function _gacha(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
function _todayIsHoliday() {
var now = new Date();
var dayOfWeek = now.getDay();
// check weekend
if (dayOfWeek == 6 || dayOfWeek == 0)
return true;
// check end of year
if (now.getMonth() == 11 && now.getDate() >= 28)
return true;
// check beginning of year
if (now.getMonth() == 0 && now.getDate() <= 4)
return true;
// check holiday
var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
var tomorrow = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);
var events = Calendar.Events.list(holidayCalId, {
timeMin: today.toISOString(),
timeMax: tomorrow.toISOString(),
singleEvents: true,
orderBy: 'startTime',
maxResults: 1
});
return events.length > 0;
}
// === below for test ===
function _getMember() {
var members = client.get("/rooms/" + roomId + "/members");
Logger.log(members.map(function(e, i, array) { return {"name":e.name, "id":e.account_id};}));
}
function _chatBot() {
client.sendMessage({
room_id: roomId, // room ID
body: "[info][title]test[/title]test[/info]"}); // message
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment