def loop(): """ 議論終了コマンドが送信されるまでメッセージの書き出しを行う関数 """ client = SlackClient(API_TOKEN) # ユーザーidからユーザー名を取得するため user_dict = get_user_dict() keys = [k for k, v in user_dict.items() if v == 'comhelper'] comhelper_user_id = keys[0] return_flag = True # 議論終了コマンドが送信されるまでループを続ける while return_flag: events = SlackClient.rtm_read(client) for event in events: event_type = event.get('type') if event_type == 'message': message_text = event.get('text') if message_text == '<@' + comhelper_user_id + "> 議論を終了して": # 議論終了コマンドが送信されたらループを抜ける return_flag = False else: # ユーザー名を確認しつつ,メッセージをテキストファイルに一時保存 send_user_id = event.get('user') send_user_name = user_dict[send_user_id] add_commit_message = [] add_commit_message.append('echo "' + send_user_name + ': ' + message_text + '" >> commit.txt') try: cmd_return = subprocess.run(add_commit_message, shell=True) except subprocess.SubprocessError: return 'echo >> commit.txt の実行でエラーが発生しました。' time.sleep(1) return '議論を終了しました!\n次の議論開始前にコミットしてください。'
import os import logging import time from slackbot.slackclient import SlackClient import dotenv logging.basicConfig(level=logging.DEBUG) dotenv.load_dotenv(verbose=True) token = os.environ["SLACKBOT_API_TOKEN"] client = SlackClient(token) client.rtm_connect() while True: events = client.rtm_read() for ev in events: print(ev) time.sleep(1) # {"type": "hello"} # {'type': 'user_typing', 'channel': 'C02M87T2E', 'user': '******'} # {'client_msg_id': 'de84cc41-17cb-4cf0-94ae-06ff52a1a8c1', 'suppress_notification': False, 'type': 'message', 'te # xt': 'hai', 'user': '******', 'team': 'T02M87T1A', 'blocks': [{'type': 'rich_text', 'block_id': 'QlOL', 'elem # ents': [{'type': 'rich_text_section', 'elements': [{'type': 'text', 'text': 'hai'}]}]}], 'user_team': 'T02M87T1A # ', 'source_team': 'T02M87T1A', 'channel': 'C02M87T2E', 'event_ts': '1577500892.003200', 'ts': '1577500892.003200 # '} # {'type': 'reaction_added', 'user': '******', 'item': {'type': 'message', 'channel': 'C02M87T2E', 'ts': '15775 # 00892.003200'}, 'reaction': 'cyclone', 'item_user': '******', 'event_ts': '1577500906.003300', 'ts': '1577500 # 906.003300'}
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.")