Package: alright
alrightPython wrapper for WhatsApp web made with selenium inspired byPyWhatsAppWhy alright ?I was looking for a way to control and automate WhatsApp web with Python, I came across some very nice libaries and wrapper implementations including;pywhatkitpywhatsappPyWhatsappWebWhatsapp-Wrapperand many othersSo I triedpywhatkit, a really cool one well crafted to be used by others but its implementations require you to open a new browser tab and scan QR code everytime you send a message no matter if its to the same person, which was deal breaker for using it.Then I triedpywhatsappwhich is based onyowsupand thus requiring you to do some registration with yowsup before using it of which after bit of googling I got scared of having my number blocked, so I went for the next optionI then went forWebWhatsapp-Wrapper, it has some good documentation and recent commits so I had hopes that it would work. But unfortunately it didn't for me, and after having a couple of errors I abandoned it to look for the next alternative.Which isPyWhatsappbyshauryauppal. This was more of CLI tool than a wrapper which suprisingly worked well and it's approach allows you to dynamically send whatsapp message to unsaved contacts without rescanning QR-code everytime.So what I did is more of a refactoring of the implementation of that tool to be more of wrapper to easily allow people to run different scripts on top of it instead of just using it as a tool. I then thought of sharing the codebase to people who might have struggled to do this as I did.Getting startedYou need to do a little bit of work to getalrightrunning, but don't worry I got you, everything will work well if you just carefully follow through the documentation.InstallationWe need to have alright installed on our machine to start using it which can either be done directly fromGitHubor usingpip.Installing directlyYou first need to clone or download the repo to your local directory and then move into the project directory as shown in the example and then run the command below;gitclonehttps://github.com/Kalebu/alrightcdalright
alright>pythonsetup.pyinstall....Installing from pippipinstallalright--upgradeSetting up SeleniumUnderneath alright isSeleniumwhich is what does all the automation work by directly controlling the browser, so you need to have a selenium driver on your machine foralrightto work. But luckily alright useswebdriver-manager, which does this automatically. You just need to install a browser. By default alright usesGoogle Chrome.What you can do with alright?Send MessagesSend Messages1Send ImagesSend VideosSend DocumentsGet first chatSearch chat by namelogoutWhen you're running your program made withalright, you can only have one controlled browser window at a time. Running a new window while another window is live will raise an error. So make sure to close the controlled window before running another oneUnsaved contact vs saved contactsAlright allows you to send the messages and media to both saved and unsaved contacts as explained earlier. But there is a tiny distinction on how you do that, you will observe this clearly as you use the package.The first step before sending anything to the user is first to locate the user and then you can start sending the information, thats where the main difference lies between saved and unsaved contacts.Saved contactsTo saved contact use methodfind_by_username()to locate a saved user. You can also use the same method to locate WhatsApp groups. The parameter can either be;saved usernamemobile numbergroup nameHere an Example on how to do that>>>fromalrightimportWhatsApp>>>messenger=WhatsApp()>>>messenger.find_by_username('saved-name or number or group')Unsaved contactsIn sending message to unsaved whatsapp contacts usefind_user()method to locate the user and the parameter can only be users number with country code with (+) omitted as shown below;>>>fromalrightimportWhatsApp>>>messenger=WhatsApp()>>>messenger.find_user('255-74848xxxx')Now Let's dive in on how we can get started on sending messages and mediasSending MessagesUse this if you don't have WhatsApp desktop installedTo send a message with alright, you first need to target a specific user by usingfind_user()method (include thecountry codein your number without the '+' symbol) and then after that you can start sending messages to the target user usingsend_message()method as shown in the example below;>>>fromalrightimportWhatsApp>>>messenger=WhatsApp()>>>messenger.find_user('2557xxxxxz')>>>messages=['Morning my love','I wish you a good night!']>>>formessageinmessages:messenger.send_message(message)Send Direct Message [NEW]RecommendedThis is newly added method that makes it a bit simpler to send a direct message without having to do thefind_userorfind_by_username, It works well even if you have or not have WhatsApp installed on your machine. It assumes the number is a saved contact by default.>>>messenger.send_direct_message(mobile,message,saved=True)It does receive the following parameters;mobile[str]- The mobile number of the user you want to send the message tomessage[str]- The message you want to sendsaved[bool]- If you want to send to a saved contact or not, default is TrueHere is an example on how to use it;>>>fromalrightimportWhatsApp>>>messenger=WhatsApp()>>>>>>messenger.send_direct_message('25573652xxx','Hello')2022-08-1417:27:57,264-root--[INFO]>>Messagesentsuccessfulyto2022-08-1417:27:57,264-root--[INFO]>>send_message()finishedrunning!>>>messenger.send_direct_message('25573652xxx','Who is This ?',False)2022-08-1417:28:30,953-root--[INFO]>>Messagesentsuccessfulyto2557365243882022-08-1417:28:30,953-root--[INFO]>>send_message()finishedrunning!Sending Messages1This Send Message does NOT find the user first like in the above Send Message, AND it does work even if you have the Desktop WhatsApp app installed.
Include thecountry codein your number withour '+' symbol as shown in the example below;>>>fromalrightimportWhatsApp>>>messenger=WhatsApp()>>>messages=['Morning my love','I wish you a good night!']>>>mobNum=27792346512>>>formessageinmessages:messenger.send_message1(mobNum,msg)Multiple numbersHere how to send a message to multiple users, Let's say we want to wish merry-x mass to all our contacts, our code is going to look like this;>>>fromalrightimportWhatsApp>>>messenger=WhatsApp()>>>numbers=['2557xxxxxx','2557xxxxxx','....']>>>fornumberinnumbers:messenger.find_user(number)messenger.send_message("I wish you a Merry X-mass and Happy new year ")You have to include thecountry codein your number for this library to work but don't include the (+) symbolIf you're sending media either picture, file, or video each of them have an optional parameter called  which is usually the caption to accompany the media.Sending ImagesSending Images is nothing new, its just the fact you have to include a path to your image and the message to accompany the image instead of just the raw string characters and also you have usesend_picture(), Here an example;>>>fromalrightimportWhatsApp>>>messenger=WhatsApp()>>>messenger.find_user('mobile')>>>messenger.send_picture('path-to-image-without-caption')>>>messenger.send_picture('path-to-image',"Text to accompany image")Sending VideosSimilarly, to send videos just use thesend_video()method;>>>fromalrightimportWhatsApp>>>messenger=WhatsApp()>>>messenger.find_user('mobile')>>>messenger.send_video('path-to-video')Sending DocumentsTo send documents such as docx, pdf, audio etc, you can use thesend_file()method to do that;>>>fromalrightimportWhatsApp>>>messenger=WhatsApp()>>>messenger.find_user('mobile')>>>messenger.send_file('path-to-file')Check if a chat has unread messages or notThis method checks if a chat, which name is passed as aqueryparameter, has got unread messages or not.>>>fromalrightimportWhatsApp>>>messenger=WhatsApp()>>>messenger.check_if_given_chat_has_unread_messages(query="Chat 123")Get first chatThis method fetches the first chat in the list on the left of the web app - since they are not ordered in an expected way, a fair workaround is applied. One can also ignore (or not ignore) pinned chats (placed at the top of the list) by passing the parameterignore_pinnedto do that - default value isignore_pinned=True.>>>fromalrightimportWhatsApp>>>messenger=WhatsApp()>>>messenger.get_first_chat()Search chat by nameThis method searches the opened chats by a partial name provided as aqueryparameter, returning the first match. Case sensitivity is treated and does not impact the search.>>>fromalrightimportWhatsApp>>>messenger=WhatsApp()>>>messenger.search_chat_by_name(query="Friend")Get last message received in a given chatThis method searches for the last message received in a given chat, received as aqueryparameter, returning the sender, text and time. Groups, numbers and contacts cases are treated, as well as possible non-received messages, video/images/stickers and others.>>>fromalrightimportWhatsApp>>>messenger=WhatsApp()>>>messenger.get_last_message_received(query="Friend")Retrieve all chat names with unread messagesThis method searches for all chats with unread messages, possibly receiving parameters tolimitthe search to atopnumber of chats or not, returning a list of chat names.>>>fromalrightimportWhatsApp>>>messenger=WhatsApp()>>>messenger.fetch_all_unread_chats(limit=True,top=30)DISCLAIMER: Apparently,fetch_all_unread_chatsfunctionallity works on most updated browser versions (for example,Chrome Version 102.0.5005.115 (Official Build) (x86_64)). If it fails with you, please consider updating your browser while we work on an alternatives for non-updated broswers.logout from whatsappYou can sign out of an account that is currently saved>>>fromalrightimportWhatsApp>>>messenger=WhatsApp()>>>messenger.logout()Well! thats all for now from the package, to request new feature make an issue.Contributionsalrightis an open-source package underMITlicense, so contributions are warmly welcome whether that be a code , docs or typo just fork it.When contributing to code please make an issue for that before making your changes so that we can have a discussion before implementation.IssuesIf you're facing any issue or difficulty with the usage of the package just raise one so that we can fix it as soon as possible.Please, be as much comprehensive as possible!Use as many screenshots and detailed description sets as possible; this will save us some time that we'd dedicate on asking you for "a more detailed descriptiton", and it'll make your request be solved faster.Give it a starWas this useful to you ? Then give it a star so that more people can manke use of this.CreditsAll the credits to:kalebuEurico NicacioVictor DanielCornelius Mostertshauryauppaland all the contributors