├── README.md ├── LICENSE ├── molibot.py └── bot.py /README.md: -------------------------------------------------------------------------------- 1 | # Slackbot -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 咸狮子 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /molibot.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | #!/usr/bin/python3 3 | import os 4 | import time 5 | import re 6 | from slackclient import SlackClient 7 | import urllib.request 8 | import requests 9 | 10 | slack_client = SlackClient('你的Slack-Token') 11 | 12 | RTM_READ_DELAY = 1 # 1 second delay between reading from RTM 13 | MENTION_REGEX = "(.*)" 14 | def parse_bot_commands(slack_events): 15 | 16 | for event in slack_events: 17 | if event["type"] == "message" and not "subtype" in event: 18 | message = parse_direct_mention(event["text"]) 19 | return message, event["channel"] 20 | return None, None 21 | 22 | def parse_direct_mention(message_text): 23 | 24 | matches = re.search(MENTION_REGEX, message_text) 25 | # the first group contains the username, the second group contains the remaining message 26 | return (matches.group(1).strip()) if matches else ( None) 27 | 28 | def handle_command(command, channel): 29 | 30 | headers = {'content-type': 'application/json'} 31 | com1 = command.replace('[', '') #将收到的消息转到给茉莉机器人api 32 | response = requests.post("""http://i.itpk.cn/api.php?question="""+com1+"""&api_key=你的Api Key&api_secret=你的Api Secret""", headers=headers) 33 | default_response = response.text #用茉莉机器人返回的数据作为默认回复。 34 | 35 | # Finds and executes the given command, filling in response 36 | response = None 37 | # This is where you start to implement more commands! 38 | slack_client.api_call( 39 | "chat.postMessage", 40 | channel=channel, 41 | text= response or default_response 42 | ) 43 | 44 | if __name__ == "__main__": 45 | if slack_client.rtm_connect(with_team_state=False): 46 | print("Starter Bot connected and running!") 47 | # Read bot's user ID by calling Web API method `auth.test` 48 | while True: 49 | command, channel = parse_bot_commands(slack_client.rtm_read()) 50 | if command: 51 | handle_command(command, channel) 52 | time.sleep(RTM_READ_DELAY) 53 | else: 54 | print("Connection failed. Exception traceback printed above.") 55 | 56 | -------------------------------------------------------------------------------- /bot.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | from slackclient import SlackClient 3 | import re 4 | import time 5 | 6 | slack_client = SlackClient('你的Token') # 在Slack API页面内获取 7 | 8 | # 常量 9 | RTM_READ_DELAY = 1 # 从RTM读取之间延迟1秒 10 | EXANPLE_COMMAND = "我是不是很帅" # 这里就是关键词,bot从Slack RTM 接收信息,并匹配,匹配成功就执行相应的if。 11 | MENTION_REGEX = "(.*)" #这是匹配全部命令的意思,会回复所有从RTM收到的消息。 12 | def parse_bot_commands(slack_events): 13 | """ 14 | 解析来自Slack RTM API的事件列表,以查找bot命令。 15 | 如果找到bot命令,则此函数返回命令和通道的元组。 16 |         如果未找到,则此函数返回None,None。 17 | """ 18 | for event in slack_events: 19 | if event["type"] == "message" and not "subtype" in event: 20 | message = parse_direct_mention(event["text"]) 21 | return message, event["channel"] 22 | return None, None 23 | 24 | def parse_direct_mention(message_text): 25 | """ 26 | 在消息文本中找到直接提及(在开头提及) 27 |         并返回提到的用户ID。 如果没有直接提及,则返回None 28 | """ 29 | matches = re.search(MENTION_REGEX, message_text) 30 | # 第一组包含用户名,第二组包含剩余消息 这里我删除了第二组以获取全部数据。 31 | return (matches.group(1).strip()) if matches else (None) 32 | 33 | def handle_command(command, channel): 34 | """ 35 | Executes bot command if the command is known 36 | """ 37 | # 默认回复 38 | default_response = "啊~ 没有找到相应的指令呢 ⊂((・x・))⊃ " 39 | 40 | # 查找并执行给定的命令,填写响应 41 | response = None 42 | # 这里是填写更多命令的地方 43 | if command.startswith(EXANPLE_COMMAND): # 这一块就是命令执行部分。 44 | response = """是的,是的。""" 45 | # 将响应发送回通道 46 | slack_client.api_call( 47 | "chat.postMessage", 48 | channel=channel, 49 | text=response or default_response 50 | ) 51 | 52 | if __name__ == "__main__": 53 | if slack_client.rtm_connect(with_team_state=False): 54 | print("Starter Bot connected and running!") 55 | # 通过调用Web API方法`auth.test`来读取bot的用户ID 56 | while True: 57 | command, channel = parse_bot_commands(slack_client.rtm_read()) 58 | if command: 59 | handle_command(command, channel) 60 | time.sleep(RTM_READ_DELAY) 61 | else: 62 | print("Connection failed. Exception traceback printed above.") 63 | 64 | --------------------------------------------------------------------------------