Skip to content

Instantly share code, notes, and snippets.

@thealanberman
Last active January 22, 2022 09:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thealanberman/4150ae1d367f98542e909728d6bb2fdc to your computer and use it in GitHub Desktop.
Save thealanberman/4150ae1d367f98542e909728d6bb2fdc to your computer and use it in GitHub Desktop.
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
Copy link
Author

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