├── requirements.txt ├── examples ├── get_messages.py ├── send_text.py ├── send_location.py ├── download_file.py ├── echo_bot.py ├── send_attachment.py ├── send_voice.py ├── send_image.py ├── send_change_keyboard.py ├── send_video.py └── send_text_with_keyboard.py ├── README.md └── client.py /requirements.txt: -------------------------------------------------------------------------------- 1 | requests 2 | sseclient-py 3 | -------------------------------------------------------------------------------- /examples/get_messages.py: -------------------------------------------------------------------------------- 1 | from sys import path 2 | path.append('..') 3 | from client import Client 4 | 5 | bot_token = 'your bot token' 6 | 7 | bot = Client(bot_token) 8 | 9 | try: 10 | messages = bot.get_messages() 11 | for message in messages: 12 | print("New message from {} \nType: {}\nBody: {}" .format(message['from'], message['type'], message['body'])) 13 | 14 | except Exception as e: 15 | print(e.args[0]) -------------------------------------------------------------------------------- /examples/send_text.py: -------------------------------------------------------------------------------- 1 | from sys import path 2 | path.append('..') 3 | from client import Client 4 | 5 | bot_token = 'your bot token' 6 | 7 | bot = Client(bot_token) 8 | 9 | try: 10 | to = 'user chat_id' 11 | 12 | [error, success] = bot.send_text(to, 'Your text') 13 | 14 | if success: 15 | print('Message sent successfully') 16 | else: 17 | print('Sending message failed: {}' .format(error)) 18 | 19 | except Exception as e: 20 | print(e.args[0]) 21 | -------------------------------------------------------------------------------- /examples/send_location.py: -------------------------------------------------------------------------------- 1 | from sys import path 2 | path.append('..') 3 | from client import Client 4 | 5 | bot_token = 'your bot token' 6 | 7 | bot = Client(bot_token) 8 | 9 | try: 10 | to = 'user chat_id' 11 | 12 | [error, success] = bot.send_location(to, 35.7448416, 51.3753212) 13 | 14 | if success: 15 | print('Message sent successfully') 16 | else: 17 | print('Sending message failed: {}' .format(error)) 18 | 19 | except Exception as e: 20 | print(e.args[0]) 21 | -------------------------------------------------------------------------------- /examples/download_file.py: -------------------------------------------------------------------------------- 1 | from sys import path 2 | path.append('..') 3 | from client import Client 4 | 5 | bot_token = 'your bot token' 6 | bot = Client(bot_token) 7 | 8 | try: 9 | file_url = 'received file url from server' 10 | save_file_path = 'path to save the file' 11 | 12 | [error, downloaded_file_path] = bot.download_file(file_url, save_file_path) 13 | 14 | if downloaded_file_path: 15 | print('file downloaded successfully in {}' .format(downloaded_file_path)) 16 | else: 17 | print('error in downloading file: {}' .format(error)) 18 | 19 | except Exception as e: 20 | print(e.args[0]) -------------------------------------------------------------------------------- /examples/echo_bot.py: -------------------------------------------------------------------------------- 1 | from sys import path 2 | path.append('..') 3 | from client import Client 4 | 5 | bot_token = 'your bot token' 6 | bot = Client(bot_token) 7 | 8 | try: 9 | messages = bot.get_messages() 10 | for message in messages: 11 | print("New message from {} \nType: {}\nBody: {}".format(message['from'], message['type'], message['body'])) 12 | message['to'] = message['from'] 13 | message.pop('from') 14 | message.pop('time') 15 | 16 | [error, success] = bot.send_message(message) 17 | 18 | if success: 19 | print('Message sent successfully') 20 | else: 21 | print('Sending message failed: {}' .format(error)) 22 | 23 | except Exception as e: 24 | print(e.args[0]) 25 | -------------------------------------------------------------------------------- /examples/send_attachment.py: -------------------------------------------------------------------------------- 1 | from sys import path 2 | path.append('..') 3 | from client import Client 4 | from os.path import getsize 5 | import ntpath 6 | 7 | bot_token = 'your bot token' 8 | 9 | bot = Client(bot_token) 10 | 11 | try: 12 | to = 'user chat_id' 13 | file_path = 'your file path' 14 | 15 | [error, file_url] = bot.upload_file(file_path) 16 | 17 | if error: 18 | print('error in uploading file: {}' .format(error)) 19 | else: 20 | print('file uploaded successfully with url: {}' .format(file_url)) 21 | 22 | [error, success] = bot.send_attachment(to, file_url, ntpath.basename(file_path), getsize(file_path), 23 | caption='your caption') 24 | 25 | if success: 26 | print('Message sent successfully') 27 | else: 28 | print('Sending message failed: {}' .format(error)) 29 | 30 | except Exception as e: 31 | print(e.args[0]) 32 | -------------------------------------------------------------------------------- /examples/send_voice.py: -------------------------------------------------------------------------------- 1 | from sys import path 2 | path.append('..') 3 | from client import Client 4 | from os.path import getsize 5 | import ntpath 6 | 7 | bot_token = 'your bot token' 8 | 9 | bot = Client(bot_token) 10 | 11 | try: 12 | to = 'user chat_id' 13 | file_path = 'your file path' 14 | 15 | file_duration_in_milliseconds = 7000 16 | 17 | [error, file_url] = bot.upload_file(file_path) 18 | 19 | if error: 20 | print('error in uploading file: {}' .format(error)) 21 | else: 22 | print('file uploaded successfully with url: {}' .format(file_url)) 23 | 24 | [error, success] = bot.send_voice(to, file_url, ntpath.basename(file_path), getsize(file_path), 25 | file_duration_in_milliseconds, caption='your caption') 26 | 27 | if success: 28 | print('Message sent successfully') 29 | else: 30 | print('Sending message failed: {}' .format(error)) 31 | 32 | except Exception as e: 33 | print(e.args[0]) 34 | -------------------------------------------------------------------------------- /examples/send_image.py: -------------------------------------------------------------------------------- 1 | from sys import path 2 | path.append('..') 3 | from client import Client 4 | from os.path import getsize 5 | import ntpath 6 | 7 | bot_token = 'your bot token' 8 | 9 | bot = Client(bot_token) 10 | 11 | try: 12 | to = 'user chat_id' 13 | image_path = 'image path' 14 | image_thumbnail_path = 'thumbnail path' 15 | 16 | [image_error, image_url] = bot.upload_file(image_path) 17 | if image_error: 18 | print('error in uploading image: {}' .format(image_error)) 19 | else: 20 | print('image uploaded successfully with url: {}' .format(image_url)) 21 | 22 | if image_url: 23 | [thumbnail_error, thumbnail_url] = bot.upload_file(image_thumbnail_path) 24 | if thumbnail_error: 25 | print('error in uploading thumbnail: {}' .format(thumbnail_error)) 26 | else: 27 | print('thumbnail uploaded successfully with url: {}' .format(thumbnail_url)) 28 | 29 | [error, success] = bot.send_image(to, image_url, ntpath.basename(image_path), getsize(image_path), 512, 512, 30 | thumbnail_url, 31 | caption='your caption') 32 | 33 | if success: 34 | print('Message sent successfully') 35 | else: 36 | print('Sending message failed: {}' .format(error)) 37 | 38 | except Exception as e: 39 | print(e.args[0]) 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Soroush Messenger Bot Python SDK 3 | Soroush Messenger Bot Wrapper for Python 4 | 5 | ## Dependencies ## 6 | - Python 2.7+ 7 | - requests 8 | - sseclient-py 9 | 10 | ## Installation ## 11 | Run the below commands 12 | ```bash 13 | git clone https://github.com/soroush-app/bot-python-sdk 14 | cd bot-python-sdk 15 | pip install -r requirements.txt 16 | ``` 17 | 18 | ## Usage ## 19 | 20 | ```python 21 | from client import Client 22 | 23 | bot_token = 'your bot token' 24 | 25 | bot = Client(bot_token) 26 | 27 | try: 28 | to = 'user chat_id' 29 | 30 | [error, success] = bot.send_text(to, 'Your text') 31 | 32 | if success: 33 | print('Message sent successfully') 34 | else: 35 | print('Sending message failed: {}' .format(error)) 36 | 37 | except Exception as e: 38 | print(e.args[0]) 39 | 40 | 41 | ``` 42 | "to" value in above example is chat_id of a bot user. You can find it in front of 'from' key in a message that user has sent to your bot. 43 | You can see more examples in the [examples](https://github.com/soroush-app/bot-python-sdk/tree/master/examples) directory. 44 | 45 | ## Contribute ## 46 | Contributions to the package are always welcome! 47 | - Report any idea, bugs or issues you find on the [issue tracker](https://github.com/soroush-app/bot-python-sdk/issues). 48 | - You can grab the source code at the package's [Git repository](https://github.com/soroush-app/bot-python-sdk.git). 49 | -------------------------------------------------------------------------------- /examples/send_change_keyboard.py: -------------------------------------------------------------------------------- 1 | from sys import path 2 | path.append('..') 3 | from client import Client 4 | 5 | bot_token = 'your bot token' 6 | 7 | bot = Client(bot_token) 8 | 9 | try: 10 | to = 'user chat_id' 11 | 12 | keyboard1 = [ 13 | [{'command': "1.1", 'text': "button 1"}, {'command': "1.2", 'text': "button 2"}], 14 | [{'command': "2.1", 'text': "button 3"}, {'command': "2.2", 'text': "button 4"}, {'command': "2.3", 'text': "button 5"}], 15 | ] 16 | keyboard2 = bot.make_keyboard('Back') 17 | keyboard3 = bot.make_keyboard('Button1|Button2|Button3') 18 | keyboard4 = bot.make_keyboard('Button1|Button2|Button3\nButton4\nButton5|Button6') 19 | keyboard5 = bot.make_keyboard([['Row1Button1', 'Row1Button2'], ['Row2Button1']]) 20 | keyboard6 = bot.make_keyboard([[['Row1Button1', 'help'], ['Row1Button2', 'back']], [['Row2Button1', 'options']]]) 21 | keyboard7 = bot.make_keyboard([[{'text': 'Row1Button1'}, {'text': 'Row1Button2'}], [{'text': 'Row2Button1'}]]) 22 | keyboard8 = bot.make_keyboard( 23 | [[{'text': 'Row1Button1', 'command': 'help'}, {'text': 'Row1Button2', 'command': 'back'}], 24 | [{'text': 'Row2Button1', 'command': 'options'}]]) 25 | 26 | [error, success] = bot.change_keyboard(to, keyboard4) 27 | 28 | if success: 29 | print('Keyboard changed successfully') 30 | else: 31 | print('Error in changing keyboard: {}' .format(error)) 32 | 33 | except Exception as e: 34 | print(e.args[0]) 35 | -------------------------------------------------------------------------------- /examples/send_video.py: -------------------------------------------------------------------------------- 1 | from sys import path 2 | path.append('..') 3 | from client import Client 4 | from os.path import getsize 5 | import ntpath 6 | 7 | bot_token = 'your bot token' 8 | 9 | bot = Client(bot_token) 10 | 11 | try: 12 | to = 'user chat_id' 13 | video_path = 'your video path' 14 | video_thumbnail_path = 'video thumbnail path' 15 | video_duration_in_milliseconds = 7000 16 | 17 | [video_error, video_url] = bot.upload_file(video_path) 18 | if video_error: 19 | print('error in uploading video: {}' .format(video_url)) 20 | else: 21 | print('video uploaded successfully with url: {}' .format(video_url)) 22 | 23 | if video_url: 24 | [thumbnail_error, thumbnail_url] = bot.upload_file(video_thumbnail_path) 25 | if thumbnail_error: 26 | print('error in uploading thumbnail: {}' .format(thumbnail_error)) 27 | else: 28 | print('thumbnail uploaded successfully with url: {}' .format(thumbnail_url)) 29 | 30 | [error, success] = bot.send_video(to, video_url, ntpath.basename(video_path), getsize(video_path), 31 | video_duration_in_milliseconds, 512, 512, 32 | thumbnail_url, 33 | caption='your caption') 34 | 35 | if success: 36 | print('Message sent successfully') 37 | else: 38 | print('Sending message failed: {}' .format(error)) 39 | 40 | except Exception as e: 41 | print(e.args[0]) 42 | -------------------------------------------------------------------------------- /examples/send_text_with_keyboard.py: -------------------------------------------------------------------------------- 1 | from sys import path 2 | 3 | path.append('..') 4 | from client import Client 5 | 6 | bot_token = 'your bot token' 7 | 8 | bot = Client(bot_token) 9 | 10 | try: 11 | to = 'user chat_id' 12 | 13 | # creating keyboard yourself 14 | keyboard1 = [ 15 | [{'command': "1.1", 'text': "button 1"}, {'command': "1.2", 'text': "button 2"}], 16 | [{'command': "2.1", 'text': "button 3"}, {'command': "2.2", 'text': "button 4"}, {'command': "2.3", 'text': "button 5"}], 17 | ] 18 | 19 | # just one button 20 | keyboard2 = bot.make_keyboard('Back') 21 | 22 | # a row with 3 buttons 23 | keyboard3 = bot.make_keyboard('Button1|Button2|Button3') 24 | 25 | # 3 rows 26 | keyboard4 = bot.make_keyboard('Button1|Button2|Button3\nButton4\nButton5|Button6') 27 | 28 | # 2 rows, each row is a list of button texts 29 | keyboard5 = bot.make_keyboard([['Row1Button1', 'Row1Button2'], ['Row2Button1']]) 30 | 31 | # 2 rows, each row is a list of button lists, each button is a list of [text, command] 32 | keyboard6 = bot.make_keyboard([[['Row1Button1', 'help'], ['Row1Button2', 'back']], [['Row2Button1', 'options']]]) 33 | 34 | # 2 rows. each row is a list of button dictionaries, each button is a dictionary os {'text':'your text'} 35 | keyboard7 = bot.make_keyboard([[{'text': 'Row1Button1'}, {'text': 'Row1Button2'}], [{'text': 'Row2Button1'}]]) 36 | 37 | # 2 rows. each row is a list of button dictionaries, each button is a dictionary os {'text': 'your text', 'command: 'your command'} 38 | keyboard8 = bot.make_keyboard( 39 | [[{'text': 'Row1Button1', 'command': 'help'}, {'text': 'Row1Button2', 'command': 'back'}], 40 | [{'text': 'Row2Button1', 'command': 'options'}]]) 41 | 42 | [error, success] = bot.send_text(to, 'Your text', keyboard=keyboard6) 43 | 44 | if success: 45 | print('Message sent successfully') 46 | else: 47 | print('Sending message failed: {}'.format(error)) 48 | 49 | except Exception as e: 50 | print(e.args[0]) 51 | -------------------------------------------------------------------------------- /client.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import sseclient 3 | import json 4 | import os 5 | from time import sleep 6 | 7 | 8 | class Client: 9 | HEADERS = {'Content-Type': 'Application/json', 'Accept': 'Application/json'} 10 | BASE_URL = 'https://bot.splus.ir/' 11 | GET_MESSAGE_URL = '/getMessage' 12 | SEND_MESSAGE_URL = '/sendMessage' 13 | DOWNLOAD_FILE_URL = '/downloadFile/' 14 | UPLOAD_FILE_URL = '/uploadFile' 15 | RETRY_DELAY = 10 16 | 17 | def __init__(self, token): 18 | self.token = token 19 | 20 | def get_upload_file_url(self): 21 | if not self.token: 22 | raise ValueError('Invalid bot token') 23 | 24 | return self.BASE_URL + self.token + self.UPLOAD_FILE_URL 25 | 26 | def get_download_file_url(self, file_url): 27 | if not self.token: 28 | raise ValueError('Invalid bot token') 29 | if not file_url: 30 | raise ValueError('Invalid file url') 31 | 32 | return self.BASE_URL + self.token + self.DOWNLOAD_FILE_URL + file_url 33 | 34 | def get_messages(self): 35 | if not self.token: 36 | raise ValueError('Invalid bot token') 37 | 38 | url = self.BASE_URL + self.token + self.GET_MESSAGE_URL 39 | 40 | while True: 41 | try: 42 | response = requests.get(url, stream=True) 43 | if 'Content-Type' in response.headers: 44 | client = sseclient.SSEClient(response) 45 | 46 | print('connected successfully\n') 47 | 48 | for event in client.events(): 49 | try: 50 | message_event = json.loads(event.data) 51 | yield message_event 52 | except Exception as e: 53 | print(e.args[0]) 54 | continue 55 | else: 56 | print('Invalid bot token OR Invalid connection response from server') 57 | 58 | print('retry to connect after 10 seconds...') 59 | sleep(self.RETRY_DELAY) 60 | 61 | except Exception as e: 62 | print(e.args[0]) 63 | print('retry to connect after 10 seconds...') 64 | sleep(self.RETRY_DELAY) 65 | continue 66 | 67 | def send_message(self, post_data): 68 | if not self.token: 69 | raise ValueError('Invalid bot token') 70 | 71 | url = self.BASE_URL + self.token + self.SEND_MESSAGE_URL 72 | 73 | post_data = json.dumps(post_data, separators=(',', ':')) 74 | 75 | try: 76 | response = requests.post(url, post_data, headers=self.HEADERS) 77 | 78 | if response: 79 | response_json = json.loads(response.text) 80 | if 'resultCode' in response_json: 81 | if response_json['resultCode'] == 200: 82 | return [False, 'OK'] 83 | else: 84 | if 'resultMessage' in response_json: 85 | return [response_json['resultMessage'], False] 86 | else: 87 | return ['Unknown Error', False] 88 | else: 89 | return ['Invalid Response', False] 90 | else: 91 | return ['Invalid Request', False] 92 | except Exception as e: 93 | return [e.args[0], False] 94 | 95 | def send_text(self, to, text, keyboard=None): 96 | 97 | post_data = { 98 | 'type': 'TEXT', 99 | 'to': to, 100 | 'body': text, 101 | } 102 | if keyboard is not None: 103 | post_data['keyboard'] = keyboard 104 | return self.send_message(post_data) 105 | 106 | def send_file(self, to, body, file_name, file_type, file_url, file_size, extra_params={}): 107 | post_data = { 108 | 'to': to, 109 | 'body': body, 110 | 'type': 'FILE', 111 | 'fileName': file_name, 112 | 'fileType': file_type, 113 | 'fileUrl': file_url, 114 | 'fileSize': file_size 115 | } 116 | 117 | for key, value in extra_params.items(): 118 | post_data[key] = value 119 | 120 | return self.send_message(post_data) 121 | 122 | def send_image(self, to, image_file_url, image_file_name, image_file_size, image_width=0, 123 | image_height=0, thumbnail_file_url=None, caption='', keyboard=None): 124 | 125 | image_file_type = 'IMAGE' 126 | extra_params = { 127 | 'imageWidth': 0, 128 | 'imageHeight': 0, 129 | 'thumbnailUrl': '' 130 | } 131 | 132 | if int(image_width) and int(image_height): 133 | extra_params['imageWidth'] = int(image_width) 134 | extra_params['imageHeight'] = int(image_height) 135 | if thumbnail_file_url: 136 | extra_params['thumbnailUrl'] = str(thumbnail_file_url) 137 | if keyboard is not None: 138 | extra_params['keyboard'] = keyboard 139 | 140 | return self.send_file(to, caption, image_file_name, image_file_type, image_file_url, image_file_size, 141 | extra_params) 142 | 143 | def send_gif(self, to, image_file_url, image_file_name, image_file_size, image_width=0, 144 | image_height=0, thumbnail_file_url=None, caption='', keyboard=None): 145 | 146 | gif_file_type = 'GIF' 147 | extra_params = { 148 | 'imageWidth': 0, 149 | 'imageHeight': 0, 150 | 'thumbnailUrl': '' 151 | } 152 | 153 | if int(image_width) and int(image_height): 154 | extra_params['imageWidth'] = int(image_width) 155 | extra_params['imageHeight'] = int(image_height) 156 | if thumbnail_file_url: 157 | extra_params['thumbnailUrl'] = str(thumbnail_file_url) 158 | if keyboard is not None: 159 | extra_params['keyboard'] = keyboard 160 | 161 | return self.send_file(to, caption, image_file_name, gif_file_type, image_file_url, image_file_size, 162 | extra_params) 163 | 164 | def send_video(self, to, video_file_url, video_file_name, video_file_size, video_duration_in_milliseconds, 165 | video_width=0, video_height=0, thumbnail_file_url=None, caption='', keyboard=None): 166 | 167 | video_file_type = 'VIDEO' 168 | extra_params = { 169 | 'thumbnailWidth': 0, 170 | 'thumbnailHeight': 0, 171 | 'thumbnailUrl': '', 172 | 'fileDuration': video_duration_in_milliseconds 173 | } 174 | 175 | if int(video_width) and int(video_height): 176 | extra_params['imageWidth'] = int(video_width) 177 | extra_params['imageHeight'] = int(video_height) 178 | if thumbnail_file_url: 179 | extra_params['thumbnailUrl'] = str(thumbnail_file_url) 180 | if keyboard is not None: 181 | extra_params['keyboard'] = keyboard 182 | 183 | return self.send_file(to, caption, video_file_name, video_file_type, video_file_url, video_file_size, 184 | extra_params) 185 | 186 | def send_voice(self, to, voice_file_url, voice_file_name, voice_file_size, voice_duration_in_milliseconds, 187 | caption='', keyboard=None): 188 | 189 | voice_file_type = 'PUSH_TO_TALK' 190 | extra_params = { 191 | 'fileDuration': voice_duration_in_milliseconds 192 | } 193 | 194 | if keyboard is not None: 195 | extra_params['keyboard'] = keyboard 196 | 197 | return self.send_file(to, caption, voice_file_name, voice_file_type, voice_file_url, voice_file_size, 198 | extra_params) 199 | 200 | def send_location(self, to, latitude, longitude, caption='', keyboard=None): 201 | 202 | post_data = { 203 | 'type': 'LOCATION', 204 | 'latitude': latitude, 205 | 'longitude': longitude, 206 | 'to': to, 207 | 'body': caption 208 | } 209 | 210 | if keyboard is not None: 211 | post_data['keyboard'] = keyboard 212 | 213 | return self.send_message(post_data) 214 | 215 | def send_attachment(self, to, file_url, file_name, file_size, caption='', keyboard=None): 216 | 217 | file_type = 'ATTACHMENT' 218 | extra_params = {} 219 | 220 | if keyboard is not None: 221 | extra_params['keyboard'] = keyboard 222 | 223 | return self.send_file(to, caption, file_name, file_type, file_url, file_size, extra_params) 224 | 225 | def change_keyboard(self, to, keyboard): 226 | 227 | post_data = { 228 | 'type': 'CHANGE', 229 | 'keyboard': keyboard, 230 | 'to': to 231 | } 232 | 233 | return self.send_message(post_data) 234 | 235 | @staticmethod 236 | def make_keyboard(keyboard_data): 237 | keyboard = [] 238 | 239 | if isinstance(keyboard_data, str): 240 | rows = keyboard_data.split('\n') 241 | for row in rows: 242 | row_keyboard = [] 243 | row_buttons = row.split('|') 244 | for button in row_buttons: 245 | if button == '': 246 | continue 247 | row_keyboard.append( 248 | { 249 | 'text': button, 250 | 'command': button 251 | } 252 | ) 253 | if row_keyboard: 254 | keyboard.append(row_keyboard) 255 | 256 | elif isinstance(keyboard_data, list): 257 | for row_data in keyboard_data: 258 | row_keyboard = [] 259 | for row_button_data in row_data: 260 | button_data = [] 261 | if isinstance(row_button_data, str): 262 | button_data = { 263 | 'text': row_button_data, 264 | 'command': row_button_data 265 | } 266 | elif isinstance(row_button_data, list): 267 | if len(row_button_data) == 1: 268 | button_data = { 269 | 'text': row_button_data[0], 270 | 'command': row_button_data[0] 271 | } 272 | elif len(row_button_data) == 2: 273 | button_data = { 274 | 'text': row_button_data[0], 275 | 'command': row_button_data[1] 276 | } 277 | elif isinstance(row_button_data, dict): 278 | if 'text' in row_button_data: 279 | if 'command' in row_button_data: 280 | button_data = { 281 | 'text': row_button_data['text'], 282 | 'command': row_button_data['command'] 283 | } 284 | else: 285 | button_data = { 286 | 'text': row_button_data['text'], 287 | 'command': row_button_data['text'] 288 | } 289 | 290 | if len(button_data): 291 | row_keyboard.append(button_data) 292 | 293 | if len(row_keyboard): 294 | keyboard.append(row_keyboard) 295 | 296 | return keyboard 297 | 298 | def download_file(self, file_url, save_file_path): 299 | if not self.token: 300 | raise ValueError('Invalid bot token') 301 | 302 | if not save_file_path: 303 | raise ValueError('Invalid path for saving file') 304 | 305 | if not file_url: 306 | raise ValueError('Invalid file url') 307 | 308 | try: 309 | response = requests.get(self.get_download_file_url(file_url)) 310 | 311 | if response.status_code == 200: 312 | try: 313 | response_json = json.loads(response.text) 314 | return [response_json['resultMessage'], False] 315 | except: 316 | pass 317 | with open(save_file_path, 'wb') as file: 318 | file.write(response.content) 319 | return [False, save_file_path] 320 | else: 321 | return ['Bad Response: ' + str(response.status_code) + ' status code', False] 322 | 323 | except Exception as e: 324 | return [e.args[0], False] 325 | 326 | def upload_file(self, file_path): 327 | if not os.path.isfile(file_path): 328 | raise ValueError('Invalid file') 329 | 330 | try: 331 | file = {'file': open(file_path, 'rb')} 332 | response = requests.post(self.get_upload_file_url(), files=file) 333 | 334 | if response.status_code == 200: 335 | if response: 336 | response_json = json.loads(response.text) 337 | if 'resultCode' in response_json: 338 | if response_json['resultCode'] == 200: 339 | if 'fileUrl' in response_json: 340 | if response_json['fileUrl']: 341 | return [False, response_json['fileUrl']] 342 | return ["Unknown Upload Error", False] 343 | else: 344 | if 'resultMessage' in response_json: 345 | return [response_json['resultMessage'], False] 346 | else: 347 | return ['Unknown Error', False] 348 | else: 349 | return ["Invalid Response", False] 350 | else: 351 | return ["Bad Response", False] 352 | else: 353 | return ['Bad Response: ' + str(response.status_code) + ' status code', False] 354 | except Exception as e: 355 | return [e.args[0], False] 356 | --------------------------------------------------------------------------------