Skip to content

Commit

Permalink
add: version 0.2.12
Browse files Browse the repository at this point in the history
  • Loading branch information
xNewz committed Oct 25, 2024
1 parent d633496 commit 429aa16
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
37 changes: 34 additions & 3 deletions one_chat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@ def send_message(to=None, bot_id=None, message=None, custom_notification=None):
return one_chat_instance.send_message(to, bot_id, message, custom_notification)


def send_file(to=None, bot_id=None, file_path=None, custom_notification=None):
if one_chat_instance is None:
raise Exception(
"OneChat is not initialized. Call init(authorization_token, to, bot_id) first."
)

to = to or default_to
bot_id = bot_id or default_bot_id

if not to or not bot_id:
raise ValueError(
"Both 'to' and 'bot_id' must be provided either during initialization or when calling this method."
)

return one_chat_instance.send_file(to, bot_id, file_path, custom_notification)


def broadcast_message(bot_id=None, to=None, message=None):
if one_chat_instance is None:
raise Exception(
Expand All @@ -48,7 +65,14 @@ def broadcast_message(bot_id=None, to=None, message=None):
return one_chat_instance.broadcast_message(bot_id, [to], message)


def send_location(to=None, bot_id=None, latitude=None, longitude=None, address=None, custom_notification=None):
def send_location(
to=None,
bot_id=None,
latitude=None,
longitude=None,
address=None,
custom_notification=None,
):
if one_chat_instance is None:
raise Exception(
"OneChat is not initialized. Call init(authorization_token, to, bot_id) first."
Expand All @@ -62,7 +86,9 @@ def send_location(to=None, bot_id=None, latitude=None, longitude=None, address=N
"Both 'to' and 'bot_id' must be provided either during initialization or when calling this method."
)

return one_chat_instance.send_location(to, bot_id, latitude, longitude, address, custom_notification)
return one_chat_instance.send_location(
to, bot_id, latitude, longitude, address, custom_notification
)


def send_sticker(to=None, bot_id=None, sticker_id=None, custom_notification=None):
Expand All @@ -81,6 +107,7 @@ def send_sticker(to=None, bot_id=None, sticker_id=None, custom_notification=None

return one_chat_instance.send_sticker(to, bot_id, sticker_id, custom_notification)


def fetch_friends_and_groups(bot_id=None):
if one_chat_instance is None:
raise Exception(
Expand All @@ -96,6 +123,7 @@ def fetch_friends_and_groups(bot_id=None):

return one_chat_instance.fetch_friends_and_groups(bot_id)


def list_all_friends(bot_id=None):
if one_chat_instance is None:
raise Exception(
Expand All @@ -111,6 +139,7 @@ def list_all_friends(bot_id=None):

return one_chat_instance.list_all_friends(bot_id)


def list_friend_ids(bot_id=None):
if one_chat_instance is None:
raise Exception(
Expand All @@ -126,6 +155,7 @@ def list_friend_ids(bot_id=None):

return one_chat_instance.list_friend_ids(bot_id)


def list_all_groups(bot_id=None):
if one_chat_instance is None:
raise Exception(
Expand All @@ -141,6 +171,7 @@ def list_all_groups(bot_id=None):

return one_chat_instance.list_all_groups(bot_id)


def list_group_ids(bot_id=None):
if one_chat_instance is None:
raise Exception(
Expand All @@ -154,4 +185,4 @@ def list_group_ids(bot_id=None):
"Bot ID must be provided either during initialization or when calling this method."
)

return one_chat_instance.list_group_ids(bot_id)
return one_chat_instance.list_group_ids(bot_id)
29 changes: 29 additions & 0 deletions one_chat/message_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,35 @@ def send_message(self, to, bot_id, message, custom_notification=None):
except requests.exceptions.RequestException as e:
return {"status": "fail", "message": f"Request failed: {str(e)}"}

def send_file(self, to, bot_id, file_path, custom_notification=None):
data = {
"to": to,
"bot_id": bot_id,
"type": "file",
}

if custom_notification:
data["custom_notification"] = custom_notification

headers = {k: v for k, v in self.headers.items() if k.lower() != "content-type"}

with open(file_path, 'rb') as file:
files = {
"file": (file_path, file),
}

try:
response = requests.post(self.base_url, headers=headers, data=data, files=files)

if response.status_code == 200:
return response.json()
else:
print(f"Response Status: {response.status_code}")
print(f"Response Content: {response.content}")
return self._handle_error(response)
except requests.exceptions.RequestException as e:
return {"status": "fail", "message": f"Request failed: {str(e)}"}

def _handle_error(self, response):
try:
error_response = response.json()
Expand Down
3 changes: 3 additions & 0 deletions one_chat/one_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def __init__(self, authorization_token):
def send_message(self, to, bot_id, message, custom_notification=None):
return self.message_sender.send_message(to, bot_id, message, custom_notification)

def send_file(self, to, bot_id, file_path, custom_notification=None):
return self.message_sender.send_file(to, bot_id, file_path, custom_notification)

def broadcast_message(self, bot_id, to, message):
return self.broadcast_sender.broadcast_message(bot_id, to, message)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name='one-chat-api', # ชื่อ package
version='0.2.11', # เวอร์ชันของ package
version='0.2.12', # เวอร์ชันของ package
description='Sending messages through One Chat API',
long_description=long_description, # รายละเอียดยาว
long_description_content_type="text/markdown", # ประเภทของเนื้อหาคำอธิบายยาว
Expand Down

0 comments on commit 429aa16

Please sign in to comment.