148
148
type: dict
149
149
description:
150
150
- Specify details to upload a file to Slack. The file can include metadata such as an initial comment, alt text, snipped and title.
151
- - See Slack's file upload API for details at U(https://api.slack.com/methods/files.getUploadURLExternal) and U(https://api.slack.com/methods/files.completeUploadExternal).
151
+ - See Slack's file upload API for details at U(https://api.slack.com/methods/files.getUploadURLExternal).
152
+ - See Slack's file upload API for details at U(https://api.slack.com/methods/files.completeUploadExternal).
152
153
suboptions:
153
154
path:
154
155
type: str
171
172
type: str
172
173
description:
173
174
- Optional title for the uploaded file.
175
+ thread_ts:
176
+ type: str
177
+ description:
178
+ - Optional timestamp of parent message to thread this message, see U(https://api.slack.com/docs/message-threading).
174
179
"""
175
180
176
181
EXAMPLES = r"""
293
298
alt_text: ''
294
299
snippet_type: ''
295
300
title: ''
301
+ thread_ts: ''
296
302
"""
297
303
298
304
import re
@@ -476,7 +482,7 @@ def do_notify_slack(module, domain, token, payload):
476
482
477
483
def get_channel_id (module , token , channel_name ):
478
484
url = SLACK_CONVERSATIONS_LIST_WEBAPI
479
- headers = {"Authorization" : f "Bearer { token } " }
485
+ headers = {"Authorization" : "Bearer " + token }
480
486
params = {
481
487
"types" : "public_channel,private_channel,mpim,im" ,
482
488
"limit" : 1000 ,
@@ -487,22 +493,22 @@ def get_channel_id(module, token, channel_name):
487
493
if cursor :
488
494
params ["cursor" ] = cursor
489
495
query = urlencode (params )
490
- full_url = f" { url } ? { query } "
496
+ full_url = "%s?%s" % ( url , query )
491
497
response , info = fetch_url (module , full_url , headers = headers , method = "GET" )
492
498
status = info .get ("status" )
493
499
if status != 200 :
494
500
error_msg = info .get ("msg" , "Unknown error" )
495
501
module .fail_json (
496
- msg = f "Failed to retrieve channels: { error_msg } (HTTP { status } )"
502
+ msg = "Failed to retrieve channels: %s (HTTP %s)" % ( error_msg , status )
497
503
)
498
504
try :
499
505
response_body = response .read ().decode ("utf-8" ) if response else ""
500
506
data = json .loads (response_body )
501
- except json . JSONDecodeError as e :
502
- module .fail_json (msg = f "JSON decode error: { e } " )
507
+ except ValueError as e :
508
+ module .fail_json (msg = "JSON decode error: %s" % str ( e ) )
503
509
if not data .get ("ok" ):
504
510
error = data .get ("error" , "Unknown error" )
505
- module .fail_json (msg = f "Slack API error: { error } " )
511
+ module .fail_json (msg = "Slack API error: %s" % error )
506
512
channels = data .get ("channels" , [])
507
513
for channel in channels :
508
514
if channel .get ("name" ) == channel_name :
@@ -511,18 +517,18 @@ def get_channel_id(module, token, channel_name):
511
517
cursor = data .get ("response_metadata" , {}).get ("next_cursor" )
512
518
if not cursor :
513
519
break
514
- module .fail_json (msg = f "Channel named '{ channel_name } ' not found." )
520
+ module .fail_json (msg = "Channel named '%s ' not found." % channel_name )
515
521
516
522
517
523
def upload_file_to_slack (module , token , channel , file_upload ):
518
524
try :
519
525
file_path = file_upload ["path" ]
520
526
if not os .path .exists (file_path ):
521
- module .fail_json (msg = f "File not found: { file_path } " )
527
+ module .fail_json (msg = "File not found: %s" % file_path )
522
528
# Step 1: Get upload URL
523
529
url = SLACK_GET_UPLOAD_URL_EXTERNAL
524
530
headers = {
525
- "Authorization" : f "Bearer { token } " ,
531
+ "Authorization" : "Bearer " + token ,
526
532
"Content-Type" : "application/x-www-form-urlencoded" ,
527
533
}
528
534
params = urlencode (
@@ -542,21 +548,21 @@ def upload_file_to_slack(module, token, channel, file_upload):
542
548
}
543
549
)
544
550
response , info = fetch_url (
545
- module , f" { url } ? { params } " , headers = headers , method = "GET"
551
+ module , "%s?%s" % ( url , params ) , headers = headers , method = "GET"
546
552
)
547
553
if info ["status" ] != 200 :
548
554
module .fail_json (
549
- msg = f "Error retrieving upload URL: { info ['msg' ]} (HTTP { info ['status' ]} )"
555
+ msg = "Error retrieving upload URL: %s (HTTP %s)" % ( info ['msg' ], info ['status' ])
550
556
)
551
557
try :
552
558
upload_url_data = json .load (response )
553
- except json . JSONDecodeError :
559
+ except ValueError :
554
560
module .fail_json (
555
- msg = f "The Slack API response is not valid JSON: { response .read ()} "
561
+ msg = "The Slack API response is not valid JSON: %s" % response .read ()
556
562
)
557
563
if not upload_url_data .get ("ok" ):
558
564
module .fail_json (
559
- msg = f "Failed to retrieve upload URL: { upload_url_data .get ('error' )} "
565
+ msg = "Failed to retrieve upload URL: %s" % upload_url_data .get ('error' )
560
566
)
561
567
upload_url = upload_url_data ["upload_url" ]
562
568
file_id = upload_url_data ["file_id" ]
@@ -573,10 +579,10 @@ def upload_file_to_slack(module, token, channel, file_upload):
573
579
)
574
580
if info ["status" ] != 200 :
575
581
module .fail_json (
576
- msg = f "Error during file upload: { info ['msg' ]} (HTTP { info ['status' ]} )"
582
+ msg = "Error during file upload: %s (HTTP %s)" % ( info ['msg' ], info ['status' ])
577
583
)
578
- except FileNotFoundError :
579
- module .fail_json (msg = f "The file { file_path } is not found." )
584
+ except IOError :
585
+ module .fail_json (msg = "The file %s is not found." % file_path )
580
586
# Step 3: Complete upload
581
587
complete_url = SLACK_COMPLETE_UPLOAD_EXTERNAL
582
588
files_data = json .dumps (
@@ -605,7 +611,7 @@ def upload_file_to_slack(module, token, channel, file_upload):
605
611
}
606
612
)
607
613
headers = {
608
- "Authorization" : f "Bearer { token } " ,
614
+ "Authorization" : "Bearer " + token ,
609
615
"Content-Type" : "application/json" ,
610
616
}
611
617
try :
@@ -614,18 +620,18 @@ def upload_file_to_slack(module, token, channel, file_upload):
614
620
)
615
621
if info ["status" ] != 200 :
616
622
module .fail_json (
617
- msg = f "Error during upload completion: { info ['msg' ]} (HTTP { info ['status' ]} )"
623
+ msg = "Error during upload completion: %s (HTTP %s)" % ( info ['msg' ], info ['status' ])
618
624
)
619
625
upload_url_data = json .load (response )
620
- except json . JSONDecodeError :
626
+ except ValueError :
621
627
module .fail_json (
622
- msg = f "The Slack API response is not valid JSON: { response .read ()} "
628
+ msg = "The Slack API response is not valid JSON: %s" % response .read ()
623
629
)
624
630
if not upload_url_data .get ("ok" ):
625
- module .fail_json (msg = f "Failed to complete the upload: { upload_url_data } " )
631
+ module .fail_json (msg = "Failed to complete the upload: %s" % upload_url_data )
626
632
return upload_url_data
627
633
except Exception as e :
628
- module .fail_json (msg = f "Error uploading file: { str (e )} " )
634
+ module .fail_json (msg = "Error uploading file: %s" % str (e ))
629
635
630
636
631
637
def main ():
@@ -647,14 +653,16 @@ def main():
647
653
blocks = dict (type = 'list' , elements = 'dict' ),
648
654
message_id = dict (type = 'str' ),
649
655
prepend_hash = dict (type = 'str' , choices = ['always' , 'never' , 'auto' ]),
650
- upload_file = dict (type = "dict" , options = dict (
651
- path = dict (type = "str" , required = True ),
652
- alt_text = dict (type = "str" ),
653
- snippet_type = dict (type = "str" ),
654
- initial_comment = dict (type = "str" ),
655
- thread_ts = dict (type = "str" ),
656
- title = dict (type = "str" )
657
- )
656
+ upload_file = dict (
657
+ type = "dict" ,
658
+ options = dict (
659
+ path = dict (type = "str" , required = True ),
660
+ alt_text = dict (type = "str" ),
661
+ snippet_type = dict (type = "str" ),
662
+ initial_comment = dict (type = "str" ),
663
+ thread_ts = dict (type = "str" ),
664
+ title = dict (type = "str" ),
665
+ )
658
666
),
659
667
),
660
668
supports_check_mode = True ,
@@ -688,7 +696,7 @@ def main():
688
696
upload_response = upload_response ,
689
697
)
690
698
except Exception as e :
691
- module .fail_json (msg = f "Failed to upload file: { str (e )} " )
699
+ module .fail_json (msg = "Failed to upload file: %s" % str (e ))
692
700
693
701
if prepend_hash is None :
694
702
module .deprecate (
0 commit comments