2025-01-31 17:32:25 +00:00
import os , sys , logging
from slack_bolt import App , Complete , Fail
from slack_bolt . adapter . socket_mode import SocketModeHandler
from dotenv import load_dotenv
from logs import log
from traceback import format_exc
2025-02-05 14:31:40 +00:00
from funcs import toBool
2025-01-31 17:32:25 +00:00
load_dotenv ( )
for requiredVar in [ " SLACK_BOT_TOKEN " , " SLACK_SIGNING_SECRET " , " PORT " ] :
if not os . environ . get ( requiredVar ) :
raise ValueError (
f ' Missing required environment variable " { requiredVar } " . Please create a .env file in the same directory as this script and define the missing variable. '
)
2025-02-05 14:31:40 +00:00
debug = toBool ( os . environ . get ( " DEBUG " ) )
if debug :
logging . basicConfig ( level = logging . DEBUG )
log ( " Establishing a connection to slack... " )
2025-01-31 17:32:25 +00:00
app = App (
token = os . environ . get ( " SLACK_BOT_TOKEN " ) ,
signing_secret = os . environ . get ( " SLACK_SIGNING_SECRET " ) ,
)
client = app . client
2025-02-05 14:31:40 +00:00
log ( " Connected to slack " )
2025-01-31 17:32:25 +00:00
@app.function ( " convert_user_to_channel " )
def useridToChannel (
inputs : dict , fail : Fail , complete : Complete , logger : logging . Logger
) :
try :
2025-01-31 18:16:58 +00:00
ids = [ inputs [ " user_id " ] , inputs [ " workflow_id " ] ]
channel_id = client . conversations_open ( users = ids ) [ " channel " ] [ " id " ]
2025-02-05 14:31:40 +00:00
if debug :
log ( f " { ids } -> { channel_id } " , " DEBUG " )
2025-01-31 17:53:12 +00:00
complete ( { " channel_id " : channel_id } )
2025-01-31 17:32:25 +00:00
except :
log ( format_exc ( ) , " ERROR " )
2025-01-31 17:53:12 +00:00
fail (
2025-01-31 17:54:43 +00:00
" An error occured app-side trying to process this workflow step. Please contact <@U06JLP2R8JV> about this issue. "
2025-01-31 17:53:12 +00:00
)
2025-01-31 17:32:25 +00:00
@app.function ( " get_message_content " )
def getMessageContent (
inputs : dict , fail : Fail , complete : Complete , logger : logging . Logger
) :
try :
result = client . conversations_history (
channel = inputs [ " channel_id " ] ,
inclusive = True ,
oldest = inputs [ " message_ts " ] ,
limit = 1 ,
)
message = result [ " messages " ] [ 0 ] [ " text " ]
2025-02-05 14:31:40 +00:00
if debug :
log ( f " { inputs [ ' channel_id ' ] } + { inputs [ ' message_ts ' ] } -> \n " + message )
2025-01-31 17:32:25 +00:00
complete ( { " message_content " : message } )
except :
log ( format_exc ( ) , " ERROR " )
fail (
2025-01-31 17:54:43 +00:00
" An error occured app-side trying to process this workflow step. Please contact <@U06JLP2R8JV> about this issue. "
2025-01-31 17:32:25 +00:00
)
if __name__ == " __main__ " :
2025-02-05 14:31:40 +00:00
log ( f " Starting bot on port { os . environ . get ( ' PORT ' ) } " )
2025-01-31 17:32:25 +00:00
app . start ( port = int ( os . environ . get ( " PORT " ) ) )