Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
fix sendImage
add downloadFileToBase64 to sendImage, sendFile
  • Loading branch information
3mora2 committed Jun 18, 2024
1 parent f2a5993 commit 073ad4e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
7 changes: 5 additions & 2 deletions WPP_Whatsapp/api/layers/HostLayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,11 @@ def asciiQr(code):
return asciiQr(code=code)

async def scrapeImg(self):
await self.ThreadsafeBrowser.page_wait_for_function("()=>document.querySelector('canvas')?.closest",
page=self.page)
try:
await self.ThreadsafeBrowser.page_wait_for_function("()=>document.querySelector('canvas')?.closest",
page=self.page)
except:
pass
click = await self.ThreadsafeBrowser.page_evaluate("""() => {
const selectorImg = document.querySelector('canvas');
const selectorUrl = selectorImg.closest('[data-ref]');
Expand Down
28 changes: 20 additions & 8 deletions WPP_Whatsapp/api/layers/SenderLayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,21 @@ async def sendMessageOptions_(self, chat, content, options=None):

async def sendImage_(self, to, filePath, filename="", caption="", quotedMessageId=None, isViewOnce=None):
to = self.valid_chatId(to)
if filePath and os.path.exists(filePath):
_base64 = self.convert_to_base64(filePath)
filename = os.path.basename(filePath) if not filename else filename
return await self.sendImageFromBase64_(to, _base64, filename, caption, quotedMessageId, isViewOnce)
else:
raise Exception("Path Not Found")
_base64 = await downloadFileToBase64(filePath, [
'image/gif',
'image/png',
'image/jpg',
'image/jpeg',
'image/webp',
])
if not _base64:
if filePath and os.path.exists(filePath):
_base64 = self.fileToBase64(filePath)
else:
raise Exception("Path Not Found")

filename = os.path.basename(filePath) if not filename else filename
return await self.sendImageFromBase64_(to, _base64, filename, caption, quotedMessageId, isViewOnce)

async def sendImageFromBase64_(self, to, _base64, filename, caption, quotedMessageId, isViewOnce,
mentionedList=None):
Expand All @@ -204,6 +213,7 @@ async def sendImageFromBase64_(self, to, _base64, filename, caption, quotedMessa
caption,
quotedMessageId,
isViewOnce,
mentionedList,
}) => {
const result = await WPP.chat.sendFileMessage(to, base64, {
type: 'image',
Expand Down Expand Up @@ -263,8 +273,10 @@ async def sendFile_(self, to, pathOrBase64, nameOrOptions, caption):
if pathOrBase64.startswith('data:'):
_base64 = pathOrBase64
else:
if pathOrBase64 and os.path.exists(pathOrBase64):
_base64 = self.convert_to_base64(pathOrBase64)
_base64 = await downloadFileToBase64(pathOrBase64)
if not _base64:
if pathOrBase64 and os.path.exists(pathOrBase64):
_base64 = self.fileToBase64(pathOrBase64)

if not options.get("filename"):
options["filename"] = os.path.basename(pathOrBase64)
Expand Down
3 changes: 2 additions & 1 deletion examples/send_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
raise Exception(creator.state)

message = "hello from wpp"
filePath="path/to/file"
filePath="https://images.unsplash.com/photo-1466442929976-97f336a657be"
# filePath=r"C:\Users\ammar\Downloads\12.jpg"
phone_number = "201016708170" # or "+201016708170"

# example
Expand Down
1 change: 0 additions & 1 deletion examples/send_text_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@
@option dict
return dict -> {'id': 'true_**********@c.us_*************_out', 'ack': 3, 'sendMsgResult': {}}
"""
client.takeScreenshot()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"the creation of any interaction, such as customer service, media sending, intelligence recognition "
"based on phrases artificial and many other things, use your imagination")

version = "0.3.0"
version = "0.3.5"

setup(
name="WPP_Whatsapp",
Expand Down

0 comments on commit 073ad4e

Please sign in to comment.