Coder Social home page Coder Social logo

sagnik20 / simple-yet-hackable-whatsapp-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from visweswaran1998/simple-yet-hackable-whatsapp-api

0.0 0.0 0.0 144 KB

There is no official WhatsApp API. Here is a simple python class which satisfies the need.

License: Apache License 2.0

Python 100.00%

simple-yet-hackable-whatsapp-api's Introduction

Simple-Yet-Hackable-WhatsApp-api

HitCount

Using This API you can achieve many things in less than 5 lines of code

Sending a message with an emoji:

from whatsapp import WhatsApp
whatsapp = WhatsApp(10)
print(whatsapp.send_message("Name",":heart: Good!"))  

Result:
Image

Running from an existing session: It it very difficult for us to scan the QR code for logging in everytime if the session is not saved, you can avoid it by adding the session parameter like this,

# SWAMI KARUPPASWAMI THUNNAI

import os
from whatsapp import WhatsApp

whatsapp = WhatsApp(100, session="mysession")
whatsapp.send_document("Thamarai", os.path.join(os.getcwd(), "message.txt"))

Your data will be saved into mysession folder from the above example. Make sure you protect your session folder.

Getting the status message of a person:

from whatsapp import WhatsApp
whatsapp = WhatsApp(10)
print(whatsapp.get_status("Name"))

Getting last seen of a person:

from whatsapp import WhatsApp
whatsapp = WhatsApp(10)
print(whatsapp.get_last_seen("Name"))

Getting the no of participants in the group:

from whatsapp import WhatsApp
whatsapp = WhatsApp(10)
result = app.participants_count_for_group("The Night Ghost Company")

creating a new group and getting invite link

# SWAMI KARUPPASWAMI THUNNAI

from whatsapp import WhatsApp
import time

app = WhatsApp(100)
parameter1: group name, parameter 2: group members
app.create_group("group", ["Thamarai", "Jeeva"])
time.sleep(10)
print(app.get_invite_link_for_group("group"))

Join the group using invite link

# SWAMI KARUPPASWAMI THUNNAI

from whatsapp import WhatsApp

app = WhatsApp(10)
app.override_timeout(30)
app.join_group("https://chat.whatsapp.com/4AIA2B3GuLp4RJOKF0M8zY")
app.send_blind_message("I am in :)")

Exiting the group

# SWAMI KARUPPASWAMI THUNNAI

from whatsapp import WhatsApp

app = WhatsApp(100)
app.exit_group("X-Test")

Sending Anonymous messages [WHATSAPP WILL BAN YOUR PHONE NUMBER IF YOU SEND MANY ANONYMOUS MESSAGES]
Note: The phone number should not contain spaces and special characters(+, -) but shall contain country code. Example: +91-861 123 4567 should be formatted to 918611234567

# SWAMI KARUPPASWAMI THUNNAI

from whatsapp import WhatsApp

app = WhatsApp(10)
app.send_anon_message("91XXXXXXXXXX", "I am sorry for what is happening sir! But I have no other choice.")

Sending Messages to multiple participants
Sometimes we need to send messages to multiple persons who may or may not be in our contact list. So we need to a combination of both send_message and send_anon_message. Here is an example where we get the contact numbers from a group and sending messages to individual persons in that group.

# SWAMI KARUPPASWAMI THUNNAI

from whatsapp import WhatsApp
import time

app = WhatsApp(100)
app.override_timeout(30)
participants = app.get_group_participants("GDG")
print(participants)

for participant in participants:
    print("Sending: ", participant)
    time.sleep(1)
    try:
        app.send_message(participant.strip(), "Hello, I'm a bot. And this is a research. Please ignore me and dont ping back")
    except Exception as e:
        print(e)
        participant = participant.replace("+", "")
        participant = participant.replace(" ", "")
        app.send_anon_message(participant.strip(), "Hello, I'm a bot. And this is a research. Please ignore me and dont ping back")
    app.goto_main()

Sending pictures to a person:

# Note of Thanks: This part of code have been taken from WhatsApp Assistant bot by Jean-Claude Tissier(@jctissier).
# Github repo-link: https://github.com/jctissier/whatsapp-assistant-bot
# License: MIT
app.send_picture("Vignesh", os.getcwd()+"/pic.jpg")

Sending documents to a person:

# SWAMI KARUPPASWAMI THUNNAI

import os
from whatsapp import WhatsApp

whatsapp = WhatsApp(100)
whatsapp.send_document("Thamarai", os.path.join(os.getcwd(), "message.txt"))

Clearing the chat:

# SWAMI KARUPPASWAMI THUNNAI

from whatsapp import WhatsApp

whatsapp = WhatsApp(100)
whatsapp.clear_chat("Test")

Getting usernames with unread messages: Will return the list of usernames for which we have yet to reply that person. Increase the count of scrolls according to your needs.

# SWAMI KARUPPASWAMI THUNNAI

from whatsapp import WhatsApp

whatsapp = WhatsApp(100, session="mysession")
print(whatsapp.unread_usernames(scrolls=1000))

and we will receive an output as a Python list. Something like this,

['Ziv Freelance Employer', 'Vignesh', 'InstagramTest', '+91 9xx36 8xxx2', 'Sundar Sir', '+91 xxx47 8xxx9']

Getting the last sent messages for a username:

# SWAMI KARUPPASWAMI THUNNAI

from whatsapp import WhatsApp

whatsapp = WhatsApp(100, session="mysession")
messages = whatsapp.get_last_message_for("Thamarai")
print(messages)

Note: I am still working on it and it might break.

Interesting use cases:

Replying to all stale messages:

# SWAMI KARUPPASWAMI THUNNAI

from whatsapp import WhatsApp

whatsapp = WhatsApp(100, session="mysession")
usernames = whatsapp.unread_usernames(scrolls=1000)
for username in usernames:
    try:
        whatsapp.send_message(username, "Hello I am a bot, apologies that my creator hasn't replied to you yet. please ping again. ")
        whatsapp.send_blind_message("Know more about me: https://github.com/VISWESWARAN1998/Simple-Yet-Hackable-WhatsApp-api")
    except:
        print("You don't have permission to send message to this group/person")

Note: Ir just automated the whatsapp, Nothing More, Nothing Less. This program is Licensed under Apache 2.0.

Thank you for so many stars on this project, you can support me in follwing ways,

  1. Paypal: [email protected]
  2. 1 GitHub follower = 1 supporter for me to get recogonized.
  3. LinkedIn skill Endorsement: https://www.linkedin.com/in/visweswaran-nagasivam-975a8b167/

simple-yet-hackable-whatsapp-api's People

Contributors

visweswaran1998 avatar friedgreenrepos avatar cristianlivella avatar imgbotapp avatar vigneshsp94 avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.