Skip to content

Instantly share code, notes, and snippets.

@thealanberman

thealanberman/code.gs

Last active Sep 5, 2020
Embed
What would you like to do?
Simple Mattermost Bot via Google Apps Script and Outgoing Webhook
/*************
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)
}
@thealanberman

This comment has been minimized.

Copy link
Owner Author

@thealanberman thealanberman commented Aug 29, 2020

This works with Mattermost 5.25.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment