コード例 #1
0
class Bot(object):
    def __init__(self):
        self._client = SlackClient(settings.API_TOKEN,
                                   bot_icon=settings.BOT_ICON if hasattr(
                                       settings, 'BOT_ICON') else None,
                                   bot_emoji=settings.BOT_EMOJI if hasattr(
                                       settings, 'BOT_EMOJI') else None)
        self._plugins = PluginsManager()
        self._dispatcher = MessageDispatcher(self._client, self._plugins,
                                             settings.ERRORS_TO)

    def run(self):
        self._plugins.init_plugins()
        self._dispatcher.start()
        if not self._client.connected:
            self._client.rtm_connect()

        _thread.start_new_thread(self._keepactive, tuple())
        logger.info('connected to slack RTM api')
        self._dispatcher.loop()

    def _keepactive(self):
        logger.info('keep active thread started')
        while True:
            time.sleep(30 * 60)
            self._client.ping()
コード例 #2
0
ファイル: bot.py プロジェクト: MyWayToJavaJunior/slackbot
class Bot(object):
    def __init__(self):
        self._client = SlackClient(
            settings.API_TOKEN,
            bot_icon=settings.BOT_ICON if hasattr(settings,
                                                  'BOT_ICON') else None,
            bot_emoji=settings.BOT_EMOJI if hasattr(settings,
                                                    'BOT_EMOJI') else None
        )
        self._plugins = PluginsManager()
        self._dispatcher = MessageDispatcher(self._client, self._plugins,
                                             settings.ERRORS_TO)

    def run(self):
        self._plugins.init_plugins()
        self._dispatcher.start()
        self._client.rtm_connect()
        _thread.start_new_thread(self._keepactive, tuple())
        logger.info('connected to slack RTM api')
        self._dispatcher.loop()

    def _keepactive(self):
        logger.info('keep active thread started')
        while True:
            time.sleep(30 * 60)
            self._client.ping()
コード例 #3
0
ファイル: bot.py プロジェクト: lowks/slackbot
class Bot(object):
    def __init__(self):
        self._client = SlackClient(settings.API_TOKEN)
        self._plugins = PluginsManager()
        self._dispatcher = MessageDispatcher(self._client, self._plugins)

    def run(self):
        self._plugins.init_plugins()
        self._dispatcher.start()
        self._client.rtm_connect()
        thread.start_new_thread(self._keepactive, tuple())
        logger.info('connected to slack RTM api')
        self._dispatcher.loop()

    def _keepactive(self):
        logger.info('keep active thread started')
        while True:
            time.sleep(30 * 60)
            self._client.ping()
コード例 #4
0
class Bot(object):
    def __init__(self, api_token=None):
        if api_token is None:
            api_token = settings.API_TOKEN
        self._client = SlackClient(api_token,
                                   bot_icon=settings.BOT_ICON if hasattr(
                                       settings, 'BOT_ICON') else None,
                                   bot_emoji=settings.BOT_EMOJI if hasattr(
                                       settings, 'BOT_EMOJI') else None)
        self._plugins = PluginsManager()
        self._dispatcher = MessageDispatcher(self._client, self._plugins)
        self._stop = threading.Event()

    def run(self):
        self._plugins.init_plugins()
        self._dispatcher.start()
        self._client.rtm_connect()
        thread.start_new_thread(self._keepactive, tuple())
        logger.info('Connected to slack RTM api')
        self._dispatcher.loop()

    def stop(self):
        self._stop.set()
        self._dispatcher.stop()

    def send_message(self, channel, message, **kwargs):
        """
      Send a message using the web API
      """
        logger.info("Send to %s: %s" % (channel, message))
        self._client.webapi.chat.post_message(channel,
                                              message,
                                              as_user=True,
                                              **kwargs)

    def _keepactive(self):
        logger.info('Start heartbeat thread')
        while not self._stop.isSet():
            self._stop.wait(30.0 * 60)
            self._client.ping()
        logger.info("Stop heartbeat thread")
コード例 #5
0
ファイル: bot.py プロジェクト: zatonovo/slackbot
class Bot(object):
    def __init__(self, api_token=None):
      if api_token is None:
        api_token = settings.API_TOKEN
      self._client = SlackClient(
        api_token,
        bot_icon = settings.BOT_ICON if hasattr(settings, 'BOT_ICON') else None,
        bot_emoji = settings.BOT_EMOJI if hasattr(settings, 'BOT_EMOJI') else None
      )
      self._plugins = PluginsManager()
      self._dispatcher = MessageDispatcher(self._client, self._plugins)
      self._stop = threading.Event()

    def run(self):
      self._plugins.init_plugins()
      self._dispatcher.start()
      self._client.rtm_connect()
      thread.start_new_thread(self._keepactive, tuple())
      logger.info('Connected to slack RTM api')
      self._dispatcher.loop()

    def stop(self):
      self._stop.set()
      self._dispatcher.stop()

    def send_message(self, channel, message, **kwargs):
      """
      Send a message using the web API
      """
      logger.info("Send to %s: %s" % (channel,message))
      self._client.webapi.chat.post_message(channel, message, as_user=True, **kwargs)

    def _keepactive(self):
      logger.info('Start heartbeat thread')
      while not self._stop.isSet():
        self._stop.wait(30.0 * 60)
        self._client.ping()
      logger.info("Stop heartbeat thread")
コード例 #6
0
            f"""\
```
{new_line.join(texts)}
```"""
        )


def fake_message(client: SlackClient, text: str, *, channel: str) -> Message:
    data = {"text": text, "channel": channel}
    return Message(client, data)


logging.basicConfig(level=logging.DEBUG)
dotenv.load_dotenv(verbose=True)
token = os.environ["SLACKBOT_API_TOKEN"]

client = SlackClient(token)
client.rtm_connect()

# message = fake_message(client, "$hello")
# hello_send(message)
res = client.webapi.channels.list(exclude_archived=True)
channels = {ch["name"]: ch["id"] for ch in res.body["channels"]}

message = fake_message(client, "$hello --name=world", channel=channels["random"])
print("@@", hello_send(message))
import time

time.sleep(1)
print("end")
コード例 #7
0
        Executes bot command if the command is known
    """
    # Default response is help text for the user
    default_response = "Not sure what you mean. Try *{}*.".format(
        EXAMPLE_COMMAND)

    # Finds and executes the given command, filling in response
    response = None
    # This is where you start to implement more commands!
    if command.startswith(EXAMPLE_COMMAND):
        response = "Sure...write some more code then I can do that!"

    # Sends the response back to the channel
    slack_client.api_call("chat.postMessage",
                          channel=channel,
                          text=response or default_response)


if __name__ == "__main__":
    if slack_client.rtm_connect(with_team_state=False):
        print("Starter Bot connected and running!")
        # Read bot's user ID by calling Web API method `auth.test`
        starterbot_id = slack_client.api_call("auth.test")["user_id"]
        while True:
            command, channel = parse_bot_commands(slack_client.rtm_read())
            if command:
                handle_command(command, channel)
            time.sleep(RTM_READ_DELAY)
    else:
        print("Connection failed. Exception traceback printed above.")