Examples
This page provides quick snippets for various tasks you can perform with WhatsFly.
Sending Messages
Text Message
whatsapp.sendMessage("33652848481", "Hello there!")
Image with Caption
To send media, you must first upload it using uploadFile, then pass the returned Upload object to sendMessage.
# 1. Upload the file
# kind can be: "image", "video", "audio", "document"
upload = whatsapp.uploadFile("path/to/image.jpg", kind="image")
# 2. Send the message with the upload
whatsapp.sendMessage("33652848481", "Look at this picture!", upload=upload)
Video
upload = whatsapp.uploadFile("path/to/video.mp4", kind="video")
whatsapp.sendMessage("33652848481", "Check this out", upload=upload)
Document
upload = whatsapp.uploadFile("path/to/report.pdf", kind="document")
whatsapp.sendMessage("33652848481", "Here is the report", upload=upload)
Reactions
React to a specific message using its JID and the sender's JID.
# jid: the chat where the message is
# message_jid: the ID of the message to react to
# sender_jid: the JID of the person who sent the original message
# reaction: the emoji
whatsapp.sendReaction(jid, message_jid, sender_jid, "👍")
Group Management
Get Group Invite Link
invite_link = whatsapp.getGroupInviteLink("1234567890@g.us")
print(f"Join here: {invite_link}")
Join Group by Invite Link
# Use the code from the end of the URL
whatsapp.joinGroupWithInviteLink("Kj9s...")
Set Group Name and Topic
group_jid = "1234567890@g.us"
whatsapp.setGroupName(group_jid, "New Group Name")
whatsapp.setGroupTopic(group_jid, "This is the new description")
Restrict Group (Admins Only)
# Only admins can send messages
whatsapp.setGroupAnnounce(group_jid, True)
# Only admins can change group info (name, icon, etc.)
whatsapp.setGroupLocked(group_jid, True)
Get Group Info
info = whatsapp.getGroupInfo("1234567890@g.us")
print(f"Group Name: {info['Name']}")
Connection Management
Check Connection Status
if whatsapp.isConnected():
print("Online")
if whatsapp.loggedIn():
print("Logged In")
Disconnect
whatsapp.disconnect()