Skip to content

Commit

Permalink
fix: broken upload_media method
Browse files Browse the repository at this point in the history
Fix broken upload media method (closes #59)
Add upload media file to tests
  • Loading branch information
filipporomani committed Nov 25, 2024
1 parent 335e01c commit e41ebc7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
13 changes: 11 additions & 2 deletions tests/async_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
phone_number_id = {1: getenv("TEST_PHONE_NUMBER_ID")}
dest_phone_number = getenv("DEST_PHONE_NUMBER")

app = AsyncWhatsApp(token=token, phone_number_id=phone_number_id, update_check=False)
app = WhatsApp(token=token, phone_number_id=phone_number_id, update_check=False)
loop = asyncio.get_event_loop()
msg = app.create_message(to=dest_phone_number, content="Hello world")

Expand Down Expand Up @@ -138,4 +138,13 @@ async def run_test():
await asyncio.sleep(60)


asyncio.run(run_test())
async def upload():
print("uploading media")
v = app.upload_media(
'C:/Users///logo.jpg',
sender=1
)
print(f"upload_media: {v}")
print("uploading media")

asyncio.run(upload())
31 changes: 12 additions & 19 deletions whatsapp/async_ext/_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,18 @@ async def upload_media(self, media: str, sender=None) -> asyncio.Future:
headers["Content-Type"] = form_data.content_type
logging.info(f"Content-Type: {form_data.content_type}")
logging.info(f"Uploading media {media}")

async def call():
async with aiohttp.ClientSession() as session:
async with session.post(
f"https://graph.facebook.com/{self.LATEST}/{sender}/media",
headers=headers,
data=form_data,
) as r:
if r.status == 200:
logging.info(f"Media {media} uploaded")
return await r.json()
logging.info(f"Error uploading media {media}")
logging.info(f"Status code: {r.status}")
logging.info(f"Response: {await r.json()}")
return await r.json()

f = asyncio.ensure_future(call())
await asyncio.sleep(0.001) # make asyncio run the task
return f
r = aiohttp.post(
f"{self.base_url}/{sender}/media",
headers=headers,
data=form_data,
)
if r.status_code == 200:
logging.info(f"Media {media} uploaded")
return r.json()
logging.info(f"Error uploading media {media}")
logging.info(f"Status code: {r.status_code}")
logging.debug(f"Response: {r.json()}") # Changed to debug level
return None


async def delete_media(self, media_id: str) -> asyncio.Future:
Expand Down

0 comments on commit e41ebc7

Please sign in to comment.