Skip to content

Commit db08ccc

Browse files
committed
Add sample code for #506
1 parent 29d4182 commit db08ccc

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# ------------------
2+
# Only for running this script here
3+
import logging
4+
import sys
5+
from os.path import dirname
6+
7+
sys.path.insert(1, f"{dirname(__file__)}/../../..")
8+
logging.basicConfig(level=logging.DEBUG)
9+
# ------------------
10+
11+
# export SLACK_API_TOKEN=xoxb-***
12+
# python3 integration_tests/samples/issues/issue_506.py
13+
14+
import os
15+
from slack import RTMClient
16+
17+
logger = logging.getLogger(__name__)
18+
global_state = {}
19+
20+
21+
@RTMClient.run_on(event="open")
22+
def open(**payload):
23+
web_client = payload["web_client"]
24+
auth_result = web_client.auth_test()
25+
global_state.update({"bot_id": auth_result["bot_id"]})
26+
logger.info(f"cached: {global_state}")
27+
28+
29+
@RTMClient.run_on(event="message")
30+
def message(**payload):
31+
data = payload["data"]
32+
if data.get("bot_id", None) == global_state["bot_id"]:
33+
logger.debug("Skipped as it's me")
34+
return
35+
# do something here
36+
web_client = payload["web_client"]
37+
message = web_client.chat_postMessage(channel=data["channel"], text="What's up?")
38+
logger.info(f"message: {message['ts']}")
39+
40+
41+
rtm_client = RTMClient(token=os.environ["SLACK_API_TOKEN"])
42+
rtm_client.start()

0 commit comments

Comments
 (0)