Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GASでSlackBot

C49eed1cf04caeb4de645d6d9117708d?s=47 takuaraki
August 22, 2018

 GASでSlackBot

Google App Scriptsを利用して勤怠管理のSlackBotを作った話です。

C49eed1cf04caeb4de645d6d9117708d?s=128

takuaraki

August 22, 2018
Tweet

Transcript

  1. ("4Ͱ4MBDL#PU  ߥ໦୎ !BSB@UBDL

  2. "CPVU.F w ߥ໦୎ !BSB@UBDL  w '63:6$PSQ w "OESPJE΍ͬͯ·͢ w

    ,PUMJOॻ͍ͯ·͢
  3. ࠷ۙͷۈଵ؅ཧ͸ 4MBDLͰ΍ͬͯ·͢

  4. ͜Μͳײ͡

  5. 4MBDLͰͷۈଵ؅ཧ w ϝʔϧΑΓ΋ָͰྑ͍ײ͡ w ࠓ೔ɺ୭͕ٳΉͷ͔͸ḪΒͳ͍ͱ෼͔Βͳ͍ w ேྱͰಉ͡νʔϜͷਓ͕Θ͟Θ͟ใࠂͯͨ͠ ˠࠓ೔ɺٳΉਓ͕4MBDLͰ௨஌͞ΕͨΒ͍͍ͳʔ

  6. ,JOUBJ#PU࡞Γ·ͨ͠ OBNF OBNF OBNF OBNF

  7. ,JOUBJ#PU w 4MBDLͰͷɺΈΜͳͷۈଵൃݴΛه࿥ w ຖேɺͦͷ೔ͷର৅ऀΛूܭ w ໊લೖྗຊจΛ·ͱΊͯ౤ߘ OBNF OBNF OBNF

    OBNF
  8. ࢖ͬͨ΋ͷ w 4MBDL0VUHPJOH8FC)PPLT w (PPHMF"QQT4DSJQU ("4  w (PPHMFεϓϨουγʔτ

  9. 0VUHPJOH8FC)PPLT w 4MBDL΁ͷಛఆͷจࣈΛؚΉ౤ߘʹ൓Ԡ͢Δ w ࢦఆͨ͠63-ʹ1045ϦΫΤετΛ౤͛Δ w ϦΫΤετύϥϝʔλ͕౤ߘϢʔβʔ໊ɺ
 ౤ߘϝοηʔδͳͲΛؚΉ

  10. (PPHMF"QQT4DSJQU w +BWBTDSJQUޓ׵ͷεΫϦϓτݴޠ w (PPHMFυΩϡϝϯτ΍εϓϨουγʔτͳͲͷ
 (PPHMF"QQTΛૢ࡞Ͱ͖Δ w ࣌ؒ΍)551ϦΫΤετΛτϦΨʔʹͯ͠
 ಈ͔͢͜ͱ͕Ͱ͖Δ

  11. γεςϜߏ੒ ("4 ۈଵه࿥ (PPHMF 4QSFBE 4IFFU ("4
 ۈଵूܭ 0VUHPJOH
 8FC)PPLT

    ۈଵೖྗ ࠓ೔ͷۈଵ͸ʁ ूܭ݁Ռදࣔ 1045
 ϦΫΤετ σʔλΛ ه࿥ σʔλΛ औಘ 1045
 ϦΫΤετ ूܭ݁ՌΛ 4MBDLʹ౤ߘ
  12. γεςϜߏ੒ ("4 ۈଵه࿥ (PPHMF 4QSFBE 4IFFU ("4
 ۈଵूܭ 0VUHPJOH
 8FC)PPLT

    ۈଵೖྗ ࠓ೔ͷۈଵ͸ʁ ूܭ݁Ռදࣔ 1045
 ϦΫΤετ σʔλΛ ه࿥ σʔλΛ औಘ 1045
 ϦΫΤετ ूܭ݁ՌΛ 4MBDLʹ౤ߘ
  13. ۈଵه࿥ɿ4MBDL w ۈଵൃݴ͸શһ΄΅ಉ͡ϑΥʔϚοτͩͬͨ
 ྫ ʲશٳʳʢਫʣࢲ༻ͷͨΊ w ʮʲʯΛτϦΨʔʹ0VUHPJOH8FC)PPLTͰ
 ه࿥༻ͷ("4ʹ1045ϦΫΤετΛૹ৴ w 1045ϦΫΤετʹ͸౤ߘऀͱຊจ͕ࡌ͍ͬͯΔ

  14. ۈଵه࿥ɿ("4 w 4MBDL͔Β1045ϦΫΤετΛड͚Δ w ຊจ͔Βਖ਼نදݱͰ೔෇Λநग़͢Δ
 ͜ͷ೔෇ΛɺٳΉ೔ͱͯ͠ѻ͏
 ྫ ʲશٳʳʢਫʣࢲ༻ͷͨΊˠ w ٳΉ೔ɺ౤ߘऀɺຊจΛεϓϨουγʔτʹه࿥

  15. ۈଵه࿥ɿ("4 function doPost(e) { // ର৅ͷ೔෇Λநग़ var pattern = /(?:\d{4}\/|)([1-9]|0[1-9]|1[0-2])\/([1-9]|0[1-9]|[12][0-9]|3[01])/;

    var matchResult = e.parameter.text.match(pattern); var date = new Date().getFullYear() + "/" + matchResult[1] + "/" + matchResult[2]; // εϓϨουγʔτʹ೔෇ɺ໊લɺຊจΛه࿥ var sheet = getCurrentYearSpreadsheet().getSheets()[0]; var kintaiValues = sheet.getSheetValues(1, 1, maxRowCount, 3); var newLineRow = -1; for (var row = 0; row < 10000; row++) { var dateCell = kintaiValues[row][0]; if (dateCell == "") { newLineRow = row + 1; break; } } arrData = [ [date, e.parameter.user_name, e.parameter.text] ]; sheet.getRange(newLineRow, 1, 1, 3).setValues(arrData); }
  16. %FNP ʮʲશٳʳʢਫʣࢲ༻ͷͨΊʯͱଧͬͯɺ εϓϨουγʔτʹ௥Ճ͞ΕΔͷΛݟͯΈΔ

  17. γεςϜߏ੒ ("4 ۈଵه࿥ (PPHMF 4QSFBE 4IFFU ("4
 ۈଵूܭ 0VUHPJOH
 8FC)PPLT

    ۈଵೖྗ ࠓ೔ͷۈଵ͸ʁ ूܭ݁Ռදࣔ 1045
 ϦΫΤετ σʔλΛ ه࿥ σʔλΛ औಘ 1045
 ϦΫΤετ ूܭ݁ՌΛ 4MBDLʹ౤ߘ
  18. γεςϜߏ੒ ("4 ۈଵه࿥ (PPHMF 4QSFBE 4IFFU ("4
 ۈଵूܭ 0VUHPJOH
 8FC)PPLT

    ۈଵೖྗ ࠓ೔ͷۈଵ͸ʁ ूܭ݁Ռදࣔ 1045
 ϦΫΤετ σʔλΛ ه࿥ σʔλΛ औಘ 1045
 ϦΫΤετ ूܭ݁ՌΛ 4MBDLʹ౤ߘ
  19. ۈଵूܭɿ4MBDL w ࠓ೔ͷۈଵ͸ʁͱ͍͏4MBDLϦϚΠϯμʔΛ
 ࢝ۀͷ෼લʹηοτ
 SFNJOE,JOUBJUPࠓ೔ͷۈଵ͸ʁBUFWFSZXFFLEBZ w ࠓ೔ͷۈଵ͸ʁΛτϦΨʔʹ0VUHPJOH8FC)PPL
 Ͱूܭ༻ͷ("4ʹ1045ϦΫΤετ

  20. ۈଵूܭɿ("4 w ۈଵه࿥ͷεϓϨουγʔτΛಡΈࠐΉ
 σʔλ͸೔෇ɺ౤ߘऀɺຊจ w ݱࡏ೔෇ͱಉ͡೔෇ͷσʔλ͚ͩநग़ w நग़ͨ͠σʔλ͔Β4MBDL౤ߘ༻ͷจΛ࡞੒ͯ͠
 4MBDLʹ౤ߘ͢Δ

  21. ۈଵूܭɿ("4 function doPost(e) { var now = new Date(); var

    slackMessage = "```\n”; var sheet = getCurrentYearSpreadsheet().getSheets()[0]; var kintaiValues = sheet.getSheetValues(1, 1, maxRowCount, 3); for (var row = 1; row < 10000; row++) { var date = kintaiValues[row][0]; if (date == "") { break; } // ݱࡏ೔෇ͱಉ͡σʔλ͚ͩநग़ͯ͠ɺ౤ߘ༻ϝοηʔδʹ௥Ճ if (date.getFullYear() == now.getFullYear() && date.getMonth() == now.getMonth() && date.getDate() == now.getDate()) { var name = kintaiValues[row][1]; var text = kintaiValues[row][2]; slackMessage += name + " " + text + "\n"; } } slackMessage += "```"; sendToSlack(slackMessage); }
  22. %FNP ʮࠓ೔ͷۈଵ͸ʁʯͱଧͬͯɺ ूܭ݁Ռ͕౤ߘ͞ΕΔͷΛݟͯΈΔ

  23. ࡞ͬͨײ૝ w (PPHMFυϥΠϒ্Ͱ׬݁ͨ͠ͷͰָͩͬͨ w (PPHMF"QQT4DSJQU͸໘ന͍ɻ͍Ζ͍ΖͰ͖ͦ͏ w #PU͸ՄѪ͍ɻ༊͞ΕΔ ࡞ͬͯΈͯ͸͍͔͕ʁ

  24. ࠓޙ΍Γ͍ͨ͜ͱ w ՄѪ໊͍લͱΞΠίϯΛ͚͍ͭͨ w ೥຤ʹόάΔ͜ͱ͕Θ͔͍ͬͯΔͷͰͳΜͱ͔͢Δ w ΑΓ៉ྷͳϑΥʔϚοτͰूܭΛ౤ߘ͢Δ w औΓফ͠ػೳΛ͚ͭΔ w

    ϒϩάʹ·ͱΊΔ