Skip to content

Commit

Permalink
fix api function
Browse files Browse the repository at this point in the history
  • Loading branch information
jakbin committed Nov 12, 2023
1 parent 9e8f933 commit c7934ed
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ from tl_bot.main import test_token
test_token(bot_token) # bot_token type str
```

### tl_bot.main.uploadd_file(bot_token, chat_id, file_name, caption)
### tl_bot.main.uploader(bot_token: str, chat_id: str, file_name: str, server_url: str, caption: str = None)

```py
from tl_bot.main import uploadd_file
Expand Down
2 changes: 1 addition & 1 deletion tl_bot/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.6"
__version__ = "0.0.7"
13 changes: 6 additions & 7 deletions tl_bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ def upload_url(bot_token: str) -> str:
config.read(config_file)
custom_server = config['Telegram']['custom_server']
if custom_server == '':
return f'https://api.telegram.org/bot{bot_token}/sendDocument'
return 'https://api.telegram.org'
else:
return f'{custom_server}/bot{bot_token}/sendDocument'
return custom_server

def uploader(bot_token: str, chat_id: str, file_name: str, caption: str = None):
def uploader(bot_token: str, chat_id: str, file_name: str, server_url: str = "https://api.telegram.org", caption: str = None):

data_to_send = []
session = requests.session()
Expand All @@ -124,7 +124,7 @@ def uploader(bot_token: str, chat_id: str, file_name: str, caption: str = None):
)

r = session.post(
upload_url(bot_token),
f"{server_url}/bot{bot_token}/sendDocument",
data=monitor,
allow_redirects=False,
headers={"Content-Type": monitor.content_type},
Expand All @@ -133,7 +133,6 @@ def uploader(bot_token: str, chat_id: str, file_name: str, caption: str = None):
try:
resp = r.json()
except JSONDecodeError:

return False

if resp['ok'] == True:
Expand All @@ -144,13 +143,13 @@ def uploader(bot_token: str, chat_id: str, file_name: str, caption: str = None):
def upload_file(bot_token: str, chat_id: str, file_name: str, caption: str = None):

file_size = os.path.getsize(file_name)

server_url = upload_url(bot_token)
custom_server = config['Telegram']['custom_server']
if custom_server == '':
if file_size > 51200000:
sys.exit("Bot can upload only 50 MB file.")

_, resp = uploader(bot_token, chat_id, file_name, caption)
_, resp = uploader(bot_token, chat_id, file_name, server_url, caption)

if resp['ok'] == True:
print(f'{file_name} uploaded sucessfully on {resp["result"]["sender_chat"]["title"]}')
Expand Down

0 comments on commit c7934ed

Please sign in to comment.