-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslackmate.py
58 lines (46 loc) · 1.74 KB
/
slackmate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
import os
import re
import logging
import socket
from modules import test
logging.basicConfig(level=logging.INFO)
slackmate_app = App(token=os.environ["SLACK_BOT_TOKEN"])
@slackmate_app.event("message")
def handle_message(payload, say):
chat_message = payload["text"]
thread_message = payload["ts"] # thread reply
if re.match(r"\b(hello|hi|hey|salut) slackmate\b", chat_message, re.IGNORECASE):
say(f"Hi <@{payload['user']}>", thread_ts=thread_message)
elif re.match(r"\bhow are you\b", chat_message, re.IGNORECASE):
say("I'm doing great, thanks for asking!",thread_ts=thread_message)
if re.match(r"\b something other\b", chat_message, re.IGNORECASE):
text = test.Test("test")
say(test.test(), thread_ts=thread_message)
@slackmate_app.event("app_mention")
def handle_mention(payload, say):
thread_message = payload["ts"]
say(f"Hi <@{payload['user']}>", thread_ts=thread_message)
@slackmate_app.command("/ip")
def handle_ipcommand(ack, body, command):
try:
ip = command["text"]
socket.inet_aton(ip)
host = socket.gethostbyaddr(ip)
response = "IP: {}, Host: {}".format(ip,host[0])
ack(response)
except socket.error:
ack("Invalid IP")
@slackmate_app.command("/host")
def handle_hostcommand(ack, body, command):
try:
host = command["text"]
ip = socket.gethostbyname(host)
response = "Host: {}, IP: {}".format(host,ip)
ack(response)
except socket.error:
ack("Invalid Hostname")
if __name__ == "__main__":
handler = SocketModeHandler(app_token=os.environ["SLACK_APP_TOKEN"], app=slackmate_app)
handler.start()