Slackbot Actor¶
In this section, let us create an actor that sends a message to a Slack Channel via webhook. Customize the channel, apparent username, and of course, the text message. You provide the Slack webhook!
Build and deploy¶
Configure a Slack webhook or request one from your organization.
The Webbook¶
How do we get the webhook into our Actor? We don’t. Instead of embedding it in the underlying container, it is defined as an environment variable in
the Actor. We must set SLACK_WEBHOOK while creating the actor. This can be done using Tapis CLI.
In the tapis actors create command, we can pass it as an environment variables via the -e flag.
Write the Actor Function¶
An example of a functional actor that says sends a slack message is:
"""Post user message to Slack"""
from agavepy.actors import get_context
import requests
import os
import simplejson as json
def post_slack(text):
"""Send a message containing text to Slack channel"""
webhook_url = os.environ.get('SLACK_WEBHOOK')
print("Actor sending message to Slack: {0}".format(text))
payload = {
'text': text,
'channel': "#2021_crt_tapis",
'username': "tapis_actors"
}
try:
requests.post(webhook_url,
data=json.dumps(payload),
headers={
"Content-type": "application/json"
}).content
except Exception as ex:
print("Exception occured", ex)
def main():
"""Main entry to grab message context from user input"""
context = get_context()
message = context['raw_message']
post_slack(message)
if __name__ == '__main__':
main()
Here we can see that we are grabbing the environment variables set from tapis actors create command.
The agavepy library includes an “actors” object which is useful to grab the message and other context from the environment.
Write the Dockerfile¶
The requirements are python, agavepy, requests simplejson python libraries, which are available through PyPi.
# pull base image
FROM python:3.6
# install requirements.txt
RUN pip3 install agavepy simplejson requests
# add the python script to docker container
ADD actor.py /actor.py
# command to run the python script
CMD ["python", "/actor.py"]
Note
Make sure to replace taccuser with your Dockerhub username.
Build Docker Container¶
Let us build and push our docker image
# Build and tag the image
$ docker build -t taccuser/slackbot-actor:1.0 .
Sending build context to Docker daemon 4.096kB
Step 1/5 : FROM python:3.7-slim
...
Successfully built b0a76425e8b3
Successfully tagged taccuser/slackbot-actor:1.0
# Push the tagged image to Docker Hub
$ docker push taccuser/slackbot-actor:1.0
The push refers to repository [docker.io/taccuser/slackbot-actor]
...
1.0: digest: sha256:67cc6f6f00589d9ae83b99d779e4893a25e103d07e4f660c14d9a0ee06a9ddaf size: 1995
Create the Actor¶
Let us pass the SLACK_WEBHOOK as an environment variable during the time of actor creation.
$ tapis actors create --repo taccuser/slackbot-actor:1.0 \
-n slackbot-actor \
-d "Send a message containing text to Slack channel" \
-e SLACK_WEBHOOK="https://hooks.slack.com/services/XXgwstwt25afaf11XX6352fwg"
+----------------+----------------------------+
| Field | Value |
+----------------+----------------------------+
| id | ww15Ex5oLxJ6b |
| name | slackbot-actor |
| owner | taccuser |
| image | taccuser/slackbot-actor:1.0|
| lastUpdateTime | 2021-08-24T14:31:58.248860 |
| status | SUBMITTED |
+----------------+----------------------------+
$ tapis actors show -v ww15Ex5oLxJ6b
{
"id": "ww15Ex5oLxJ6b",
"name": "slackbot-actor",
"description": "Send a message containing text to Slack channel",
"owner": "taccuser",
"image": "taccuser/slackbot-actor:1.0",
"createTime": "2021-08-25T14:04:42.819Z",
"lastUpdateTime": "2021-08-25T14:04:42.819Z",
"defaultEnvironment": {
"SLACK_WEBHOOK"="https://hooks.slack.com/services/XXgwstwt25afaf11XX6352fwg"
},
"gid": 862347,
"hints": [],
"link": "",
"mounts": [
{
"container_path": "/work",
"host_path": "/work",
"mode": "rw"
},
{
"container_path": "/corral",
"host_path": "/corral-repl/projects/SD2E-Community",
"mode": "rw"
}
],
"privileged": false,
"queue": "default",
"stateless": true,
"status": "READY",
"statusMessage": " ",
"token": true,
"uid": 862347,
"useContainerUid": false,
"webhook": "",
"_links": {
"executions": "https://api.sd2e.org/actors/v2/ww15Ex5oLxJ6b/executions",
"owner": "https://api.sd2e.org/profiles/v2/sgopal",
"self": "https://api.sd2e.org/actors/v2/ww15Ex5oLxJ6b"
}
}
Above, you can see the plain text name, description, defaultEnvironment that were passed on the command line. In addition, you can see the “status” of the actor is “READY”, meaning it is ready to receive and act on messages.
Finally, you can list all actors visible to you with:
$ tapis actors list
+---------------+---------------+----------+-----------------------------+----------------------------+--------+
| ww15Ex5oLxJ6b | slackbot-actor| taccuser | taccuser/slackbot-actor:1.0 | 2021-08-25T14:04:42.819Z | READY |
+---------------+---------------+----------+-----------------------------+----------------------------+--------+
Submit a Message to the Actor¶
# Write a message
$ export MESSAGE='Hello, Slack!'
$ echo $MESSAGE
Hello, Slack!
# Submit the message to the actor
$ tapis actors submit -m "$MESSAGE" ww15Ex5oLxJ6b
+-------------+---------------+
| Field | Value |
+-------------+---------------+
| executionId | EjO6yw03GKRmR |
| msg | Hello, Slack |
+-------------+---------------+
Let us grab the executionId from here to track the progress of the actor.
List Executions of Actor¶
The above execution has already completed. Show detailed information for the execution with:
$ tapis actors execs show -v ww15Ex5oLxJ6b EjO6yw03GKRmR
{
"actorId": "ww15Ex5oLxJ6b",
"apiServer": "https://api.sd2e.org",
"cpu": 117091281,
"exitCode": 1,
"finalState": {
"Dead": false,
"Error": "",
"ExitCode": 1,
"FinishedAt": "2021-08-25T14:10:19.308Z",
"OOMKilled": false,
"Paused": false,
"Pid": 0,
"Restarting": false,
"Running": false,
"StartedAt": "2021-08-25T14:10:18.918Z",
"Status": "exited"
},
"id": "EjO6yw03GKRmR",
"io": 90,
"messageReceivedTime": "2021-08-25T14:10:17.491Z",
"runtime": 1,
"startTime": "2021-08-25T14:10:18.436Z",
"status": "COMPLETE",
"workerId": "ww1zAwBG5R7MQ",
"_links": {
"logs": "https://api.sd2e.org/actors/v2/ww15Ex5oLxJ6b/executions/EjO6yw03GKRmR/logs",
"owner": "https://api.sd2e.org/profiles/v2/sgopal",
"self": "https://api.sd2e.org/actors/v2/ww15Ex5oLxJ6b/executions/EjO6yw03GKRmR"
}
}
Check the Logs for an Execution¶
In our slackbot-actor, we expect the actor to print the message passed to it and notify on the slack channel.
$ tapis actors execs logs ww15Ex5oLxJ6b EjO6yw03GKRmR
Logs for execution EjO6yw03GKRmR
Actor sending message to Slack: Hello, Slack!
Finally check your Slack channel to find your message! Did you find it? Hooray !