大家好,几天前,我正在考虑使用python从WhatsApp发送消息。就像你们一样,我开始潜伏在互联网上寻找一些解决方案吐里奥。在一开始,这似乎是一个很好的解决方案,但它不是免费的,我不得不购买一个Twilio电话号码。而且,我无法在互联网上找到任何WhatsAppAPI。因此,我放弃了使用Twilio和任何其他WhatsAppAPI的想法。想了很多之后,我累了,打开了Android工作室,开始开发我的一个应用程序。我接通了我的电话,然后开始了这个过程。当这个应用程序构建的时候,我想到了一个想法,那就是用手机本身来实现WhatsApp消息传递的自动化。我搜索了一下同样的东西,发现了一些有很大潜力解决我的问题的东西。我找到了一个命令行工具adb
这有助于一个人控制电话而不碰手机。
adb devices
。您将在附加设备列表中看到设备。让我们首先导入一些我们需要的依赖项。您可以使用pip
.
import cv2 import subprocess
我们将需要子进程通过命令行调用亚行并获得输出,我们需要CV2进行一些图像处理,以便python能够单击屏幕或任何其他任务。
现在让我们在下面创建一个名为亚行的基本函数,
def adb(command): proc = subprocess.Popen(command.split(' '), stdout=subprocess.PIPE, shell=True) (out, _) = proc.communicate() return out.decode('utf-8')
基本上,上面的功能是通过子进程调用亚行,并检索我们需要的输出。
现在让我们编写代码,python将在其中单击移动设备的屏幕。因此,我们将创建一个名为TAP的函数,它将单击屏幕上的特定位置。
def tap(tap_x, tap_y): adb("adb shell input tap {} {}".format(tap_x, tap_y)) tap(100,100)
这将从x单击100 px,从y单击100 px。现在您一定认为很难对每个命令的坐标进行硬编码,当设备被更改时,它将无法工作,这就是为什么在这个博客的下一部分中,我们将使用图像处理来自动检测坐标。
def take_screenshot(final): adb(f"adb exec-out screencap -p > ./images/{final}.png")
代码很简单。我们已经做了一个功能,保存屏幕截图内的移动图像目录。在函数中,我们可以传递图像文件的名称。
现在,我们将使用目标图像来自动检测坐标,而不是传递坐标。为了更好地理解这一点,让我们举个例子,我有这个屏幕
我想打开我们之间的应用程序,然后使用一个名为TemplateMatching
。在这个过程中,我们将获取屏幕的屏幕截图>使用模板匹配计算我们之间图标的坐标>单击那里
# Here small_image is among us icon and big_image is the complete screenshot containing the among us icon def image_position(small_image, big_image): img_rgb = cv2.imread(big_image) img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY) template = cv2.imread(small_image, 0) height, width = template.shape[::] res = cv2.matchTemplate(img_gray, template, cv2.TM_SQDIFF) _, _, top_left, _ = cv2.minMaxLoc(res) bottom_right = (top_left[0] + width, top_left[1] + height) return (top_left[0]+bottom_right[0])//2, (top_left[1]+bottom_right[1])//2 screen="screen" take_screenshot(screen) x, y = image_position("images/among_us_icon.png", f"images/{screen}") click(x,y) # WOWWW Python successfully opened among us app.
使用上面的代码,即使您在移动屏幕上更改了我们游戏的位置,python仍然能够打开游戏。
对于亚行和python,还有更多的事情可以做。让我们谈谈他们中的一些。
def swipe(start_x, start_y, end_x, end_y, duration_ms): adb("adb shell input swipe {} {} {} {} {}".format(start_x, start_y, end_x, end_y, duration_ms))
def call(number): adb(f"adb shell am start -a android.intent.action.CALL -d tel:{number}") call('+91xxxxxxxxxx') # +[CODE][NUMBER]
def download(path, output_path): adb(f"adb pull {path} {output_path}")
def remove(path): adb(f"adb shell rm {path}") #/sdcard/...
# name is the video_file name and time is the seconds you want to record def screen_record(name, time): adb(f"adb shell screenrecord /sdcard/{name} --time-limit {time}") download(f"/sdcard/{name}",f"./mobile/{name}") remove(f"/sdcard/{name}")
def switch_phone_on_off(): adb("adb shell input keyevent 26")
还有更多的关键事件,比如26。如果你想知道,那就去看看这链接。
def open_url(url): adb(f'adb shell am start -a android.intent.action.VIEW -d {url}') open_url("https://www.google.co.in/")
好吧,所以我觉得这很酷。在获得了所有这些基本理解之后,我们得出了我的主要问题,即发送WhatsApp消息时没有QR代码,也没有像Twilio这样的付费方法。有点棘手,但在我手机上有效。我希望它也适用于你的。
def send_whatsapp_message(phone, message): adb(f'adb shell am start -a android.intent.action.VIEW -d "https://api.whatsapp.com/send?phone={phone}"') # Opening whatsapp url adb('ping 127.0.0.1 -n 2 > nul') # delay adb(f'adb shell input text "{message}"') # entering message adb('adb shell keyevent 22') # Right arrow adb('adb shell keyevent 22') # Right arrow adb('adb shell input keyevent 22') # Right arrow adb('adb shell input keyevent 22') # Right arrow adb('adb shell input keyevent 66') # Enter Key send_whatsapp_message('+91xxxxxxxxxx', 'blah blah blah')
消息发了!
今天就是这样。我希望你喜欢我在博客上发布的内容,它为你的一天增添了一些价值。这是我的第一篇博文,我以前对我的语法,拼写,语法错误等等都很紧张。但我喜欢开发人员在开发方面互相帮助。所以现在是我帮助别人和传播我所知道的东西的时候了。话虽如此,我想在这里结束这个博客,并希望再次见到你。