예제 #1
0
 def setUp(self):
     setup_mock_web_api_server(self)
     self.rtm = RTMClient(
         token="xoxp-1234",
         base_url="http://localhost:8888",
         auto_reconnect_enabled=False,
     )
예제 #2
0
    def test_start_raises_an_error_if_rtm_ws_url_is_not_returned(self):
        with self.assertRaises(e.SlackApiError) as context:
            RTMClient(token="xoxp-1234", auto_reconnect_enabled=False).start()

        expected_error = (
            "The request to the Slack API failed.\n"
            "The server responded with: {'ok': False, 'error': 'invalid_auth'}"
        )
        self.assertIn(expected_error, str(context.exception))
예제 #3
0
def handle(client: RTMClient, event: dict):
    print(event)
    client = WebClient(token=slack_token)
    try:
        response = client.chat_postMessage(channel='#general',
                                           text="Hello world!")
        assert response["message"]["text"] == "Hello world!"
    except SlackApiError as e:
        # You will get a SlackApiError if "ok" is False
        assert e.response["ok"] is False
        assert e.response[
            "error"]  # str like 'invalid_auth', 'channel_not_found'
        print(f"Got an error: {e.response['error']}")
예제 #4
0
def run():

    try:
        config_file = os.environ.get(
            "ART_BOT_SETTINGS_YAML",
            f"{os.environ['HOME']}/.config/art-bot/settings.yaml")
        with open(config_file, 'r') as stream:
            bot_config.update(yaml.safe_load(stream))
    except yaml.YAMLError as exc:
        print(f"Error reading yaml in file {config_file}: {exc}")
        exit(1)
    except Exception as exc:
        print(f"Error loading art-bot config file {config_file}: {exc}")
        exit(1)

    def abs_path_home(filename):
        # if not absolute, relative to home dir
        return filename if filename.startswith(
            "/") else f"{os.environ['HOME']}/{filename}"

    try:
        with open(abs_path_home(bot_config["slack_api_token_file"]),
                  "r") as stream:
            bot_config["slack_api_token"] = stream.read().strip()
    except Exception as exc:
        print(
            f"Error: {exc}\nYou must provide a slack API token in your config. You can find this in bitwarden."
        )
        exit(1)

    logging.basicConfig()
    logging.getLogger('activemq').setLevel(logging.DEBUG)

    rtm_client = RTMClient(token=bot_config['slack_api_token'])
    rtm_client.on("hello")(on_load)
    rtm_client.on("message")(incoming_message)

    if "umb" in bot_config:
        # umb listener setup is optional

        bot_config["umb"].setdefault("env", "stage")
        bot_config["umb"].setdefault("ca_certs_file", umb.DEFAULT_CA_CHAIN)
        bot_config["umb"].setdefault("client_id", "openshift-art-bot-slack")
        try:
            if bot_config["umb"]["env"] not in ["dev", "stage", "prod"]:
                raise Exception(
                    f"invalid umb env specified: {bot_config['umb']['env']}")
            for umbfile in [
                    "client_cert_file", "client_key_file", "ca_certs_file"
            ]:
                if not bot_config["umb"].get(umbfile, None):
                    raise Exception(
                        f"config must specify a file for umb {umbfile}")
                bot_config["umb"][umbfile] = abs_path_home(
                    bot_config["umb"][umbfile])
                if not os.path.isfile(bot_config["umb"][umbfile]):
                    raise Exception(
                        f"config specifies a file for umb {umbfile} that does not exist: {bot_config['umb'][umbfile]}"
                    )
        except Exception as exc:
            print(f"Error in umb configuration: {exc}")
            exit(1)

        clair_consumer = consumer_start('eng.clair.>', clair_consumer_callback)
        clair_consumer.join()

    rtm_client.start()
예제 #5
0
import os
from slack_sdk.web import WebClient
from slack_sdk.rtm.v2 import RTMClient
from slack_sdk.errors import SlackApiError

slack_token = "[API_TOKEN]"
slack_bot_token = "[BOT_API_TOKEN]"

rtm = RTMClient(token=slack_bot_token)


@rtm.on("message")
def handle(client: RTMClient, event: dict):
    print(event)
    client = WebClient(token=slack_token)
    try:
        response = client.chat_postMessage(channel='#general',
                                           text="Hello world!")
        assert response["message"]["text"] == "Hello world!"
    except SlackApiError as e:
        # You will get a SlackApiError if "ok" is False
        assert e.response["ok"] is False
        assert e.response[
            "error"]  # str like 'invalid_auth', 'channel_not_found'
        print(f"Got an error: {e.response['error']}")


print("Bot is up and running!")
rtm.start()
예제 #6
0
class TestRTMClient(unittest.TestCase):
    def setUp(self):
        setup_mock_web_api_server(self)
        self.rtm = RTMClient(
            token="xoxp-1234",
            base_url="http://*****:*****@self.rtm.on("message")
        def fn2(client, payload):
            pass

        self.assertIsNotNone(fn1)
        self.assertIsNotNone(fn2)
        self.assertEqual(fn2.__name__, "fn2")

    def test_run_on_annotation_sets_callbacks(self):
        @self.rtm.on("message")
        def say_run_on(client, payload):
            pass

        self.assertTrue(len(self.rtm.message_listeners) == 1)

    def test_on_sets_callbacks(self):
        def say_on(client, payload):
            pass

        self.rtm.on("message")(say_on)
        self.assertTrue(len(self.rtm.message_listeners) == 1)

    def test_on_accepts_a_list_of_callbacks(self):
        def say_on(client, payload):
            pass

        def say_off(client, payload):
            pass

        self.rtm.on("message")(say_on)
        self.rtm.on("message")(say_off)
        self.assertEqual(len(self.rtm.message_listeners), 2)

    def test_on_raises_when_not_callable(self):
        invalid_callback = "a"

        with self.assertRaises(e.SlackClientError) as context:
            self.rtm.on("message")(invalid_callback)

        expected_error = "The listener 'a' is not a Callable (actual: str)"
        error = str(context.exception)
        self.assertIn(expected_error, error)

    def test_on_raises_when_kwargs_not_accepted(self):
        def invalid_cb():
            pass

        with self.assertRaises(e.SlackClientError) as context:
            self.rtm.on("message")(invalid_cb)

        error = str(context.exception)
        self.assertIn(
            "The listener 'invalid_cb' must accept two args: client, event (actual: )",
            error,
        )

    def test_send_over_websocket_raises_when_not_connected(self):
        with self.assertRaises(e.SlackClientError) as context:
            self.rtm.send(payload={})

        expected_error = "The RTM client is not connected to the Slack servers"
        error = str(context.exception)
        self.assertIn(expected_error, error)

    def test_start_raises_an_error_if_rtm_ws_url_is_not_returned(self):
        with self.assertRaises(e.SlackApiError) as context:
            RTMClient(token="xoxp-1234", auto_reconnect_enabled=False).start()

        expected_error = (
            "The request to the Slack API failed.\n"
            "The server responded with: {'ok': False, 'error': 'invalid_auth'}"
        )
        self.assertIn(expected_error, str(context.exception))
    datefmt="%Y-%m-%d %H:%M:%S",
)
logger = logging.getLogger(__name__)

import os
from slack_sdk.rtm.v2 import RTMClient
from integration_tests.env_variable_names import SLACK_SDK_TEST_CLASSIC_APP_BOT_TOKEN

# pip3 install proxy.py
# proxy --port 9000 --log-level d
proxy_url = "http://*****:*****@rtm.on("message")
    def handle(client: RTMClient, event: dict):
        client.web_client.reactions_add(
            channel=event["channel"],
            timestamp=event["ts"],
            name="eyes",
        )

    @rtm.on("*")
    def handle(client: RTMClient, event: dict):
        logger.info(event)