-
-
Save Sintayew4/19d1e48cd13e611c9f0375fa081dc765 to your computer and use it in GitHub Desktop.
Simple Mattermost Bot via Google Apps Script and Outgoing Webhook
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
/************* | |
MATTERMOST OUTGOING WEBHOOK BOT | |
INSTRUCTIONS | |
Create a Google Spreadsheet | |
In column A of Sheet1, put a list of however many potential responses you'd like. | |
Spreadsheet menu > Tools > Script Editor | |
Paste in this entire file of code as the 'code.gs' | |
Script Editor menu > File > Project Properties > Script Properties | |
Add a row with key name 'verifyToken' and value of the webhook token. | |
Customize the trigger word below. | |
Script Editor menu > Publish > Deploy as web app... | |
Grab the URL and put that in the outgoing webhook configuration. | |
[OPTIONAL] | |
Include BetterLog (https://github.com/peterherrmann/BetterLog) | |
menu > Resources > Libraries... | |
Find a Library > project key MYB7yzedMbnJaMKECt6Sm7FLDhaBgl_dE | |
Choose the latest version in the dropdown > Save | |
**************/ | |
Logger = BetterLog.useSpreadsheet(SpreadsheetApp.getActiveSpreadsheet().getId()) | |
var trigger_word = "!mytrigger" | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1") | |
var range = sheet.getDataRange() | |
var values = range.getValues() | |
var rows = range.getNumRows() | |
// respond to HTTP POST requests | |
function doPost(e) { | |
Logger.log("e = "+JSON.stringify(e)) | |
//Convert incoming message to JSON | |
var incoming = JSON.parse(e.postData.contents) | |
Logger.log("incoming = "+JSON.stringify(incoming)) | |
//Get Script properties. | |
var prop = PropertiesService.getScriptProperties().getProperties() | |
if (prop.verifyToken != incoming.token) { | |
throw new Error("invalid token.") | |
} | |
if (incoming.text.toLowerCase().indexOf(trigger_word) > -1) { | |
// randomly select a quote from the array pulled from the spreadsheet | |
var i = Math.floor(Math.random() * rows) | |
var message = values[i][0] | |
} | |
output = JSON.stringify({"text": message}) | |
return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.JSON) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment