Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Slack chat-bot Lambda handler.
import os
import logging
import urllib
# Grab the Bot OAuth token from the environment.
BOT_TOKEN = os.environ["BOT_TOKEN"]
# Define the URL of the targeted Slack API resource.
# We'll send our replies there.
def lambda_handler(data, context):
"""Handle an incoming HTTP request from a Slack chat-bot.
# Grab the Slack event data.
slack_event = data['event']
# We need to discriminate between events generated by
# the users, which we want to process and handle,
# and those generated by the bot.
if "bot_id" in slack_event:
logging.warn("Ignore bot event")
# Get the text of the message the user sent to the bot,
# and reverse it.
text = slack_event["text"]
reversed_text = text[::-1]
# Get the ID of the channel where the message was posted.
channel_id = slack_event["channel"]
# We need to send back three pieces of information:
# 1. The reversed text (text)
# 2. The channel id of the private, direct chat (channel)
# 3. The OAuth token required to communicate with
# the API (token)
# Then, create an associative array and URL-encode it,
# since the Slack API doesn't not handle JSON (bummer).
data = urllib.parse.urlencode(
("token", BOT_TOKEN),
("channel", channel_id),
("text", reversed_text)
data = data.encode("ascii")
# Construct the HTTP request that will be sent to the Slack API.
request = urllib.request.Request(
# Add a header mentioning that the text is URL-encoded.
# Fire off the request!
# Everything went fine.
return "200 OK"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment