forked from OWASP-BLT/BLT-Lettuce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
122 lines (99 loc) · 4.9 KB
/
app.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from flask import Flask, request,jsonify
from slack import WebClient
from slackeventsapi import SlackEventAdapter
from dotenv import load_dotenv
import logging
import os
import git
from slack_sdk.errors import SlackApiError
from slack_bolt import App
import sqlite3
load_dotenv()
logging.basicConfig(filename='slack_messages.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
app = Flask(__name__)
slack_events_adapter = SlackEventAdapter(os.environ['SIGNING_SECRET'], "/slack/events", app)
client = WebClient(token=os.environ['SLACK_TOKEN'])
client.chat_postMessage(channel='#project-blt-lettuce-deploys', text=f"bot started v1.7 top")
# keep for debugging purposes
#@app.before_request
#def log_request():
# if request.path == '/slack/events' and request.method == 'POST':
# # Log the request headers and body
# logging.info(f"Headers: {request.headers}")
# logging.info(f"Body: {request.get_data(as_text=True)}")
@app.route('/update_server', methods=['POST'])
def webhook():
# check if there is an API key -
if request.method == 'POST':
current_directory = os.path.dirname(os.path.abspath(__file__))
repo = git.Repo(current_directory)
origin = repo.remotes.origin
origin.pull()
latest_commit = repo.head.commit
latest_commit_message = latest_commit.message.strip()
client.chat_postMessage(channel='#project-blt-lettuce-deploys', text=f"Deployed the latest version 1.8. Latest commit: {latest_commit_message}")
return 'OK', 200
else:
return 'Error', 400
@slack_events_adapter.on("team_join")
def handle_team_join(event_data):
user_id = event_data["event"]["user"]["id"]
# private channel for joins so it does not get noisy
response = client.chat_postMessage(channel='C06RMMRMGHE', text=f"<@{user_id}> joined the team.")
if not response["ok"]:
client.chat_postMessage(channel='#project-blt-lettuce-deploys', text=f"Error sending message: {response['error']}")
logging.error(f"Error sending message: {response['error']}")
@slack_events_adapter.on("member_joined_channel")
def handle_member_joined_channel(event_data):
event = event_data["event"]
user_id = event["user"]
channel_id = event["channel"]
# send a message to the user if they joined the #owasp-community channel
client.chat_postMessage(channel=channel_id, text=f"Welcome <@{user_id}> to the <#{channel_id}> channel!")
@app.command("/setcrypto")
def set_crypto_command(ack, say, command):
ack()
user_id = command["user_id"]
crypto_name, address = command["text"].split()
# Connect to the SQLite database
conn = sqlite3.connect('crypto_addresses.db')
cursor = conn.cursor()
# Insert the user's data into the database
cursor.execute("INSERT INTO addresses (user_id, crypto_name, address) VALUES (?, ?, ?)", (user_id, crypto_name, address))
conn.commit()
conn.close()
say(f"Your cryptocurrency address for {crypto_name} has been saved.")
return jsonify({
"response_type": "in_channel",
"text": f"Your cryptocurrency address for {crypto_name} has been saved."
})
@slack_events_adapter.on("message")
def handle_message(payload):
message = payload.get("event", {})
try:
response = client.auth_test()
bot_user_id = response["user_id"]
except SlackApiError as e:
bot_user_id = None
# Check if the message was not sent by the bot itself
if message.get("user") != bot_user_id:
if (message.get("subtype") is None and
not any(keyword in message.get("text", "").lower() for keyword in ["#contribute"]) and
any(keyword in message.get("text", "").lower() for keyword in ["contribute", "contributing", "contributes"])):
user = message.get("user")
channel = message.get("channel")
logging.info(f"detected contribute sending to channel: {channel}")
response = client.chat_postMessage(channel=channel, text=f"Hello <@{user}>! Please check this channel <#C04DH8HEPTR> for contributing guidelines today!")
if not response["ok"]:
client.chat_postMessage(channel='#project-blt-lettuce-deploys', text=f"Error sending message: {response['error']}")
logging.error(f"Error sending message: {response['error']}")
if message.get('channel_type') == 'im':
user = message['user'] # The user ID of the person who sent the message
text = message.get('text', '') # The text of the message
try:
if message.get("user") != bot_user_id:
client.chat_postMessage(channel='C06RMMRMGHE', text=f"<@{user}> said {text}")
# Respond to the direct message
client.chat_postMessage(channel=user, text=f"Hello <@{user}>, you said: {text}")
except SlackApiError as e:
print(f"Error sending response: {e.response['error']}")