菜狗子学Python:给好友发微信

转载请标明来源(https://blog.csdn.net/SingingFisher/article/details/96283838)

前言

我的朋友向我借钱不还,我想要夸夸他,于是写了这个程序。

代码

#!/usr/bin/env python
# encoding: utf-8
"""
@author: 丁涛
@file: wx.py
@time: 2019/7/5 13:02
@desc:
"""
from threading import Timer
from wxpy import *
from tkinter.filedialog import askopenfile
import random

import tkinter as tk


def select_path():
    path_ = askopenfile()
    path.set(path_.name)


window = tk.Tk()
window.title('夸人宝')
window.geometry('600x600')

tk.Label(window, text='发送间隔时间(单位秒)').place(x=10, y=10)
interval_time = tk.StringVar()
tk.Entry(window, textvariable=interval_time).place(x=320, y=10)

tk.Label(window, text='词库中每个种子开头符号(没有则不输入)').place(x=10, y=40)
prefix = tk.StringVar()
tk.Entry(window, textvariable=prefix).place(x=320, y=40)

tk.Label(window, text='词库地址(每行需要由@#$%&英文特殊符号中的一个开头)').place(x=10, y=70)
path = tk.StringVar()
tk.Entry(window, textvariable=path).place(x=320, y=70)
tk.Button(window, text='选择词库', command=select_path).place(x=500, y=70)



friend_x = 10
friend_y = 135
friend_y_skip = 30

friends = []
words = []


def add_send_friend():
    global friend_y
    friend_string = tk.StringVar()
    tk.Entry(window, textvariable=friend_string).place(x=friend_x, y=friend_y)
    friend_y += friend_y_skip
    friends.append(friend_string)


def send_msg():
    global friends
    prefix_ = prefix.get()
    with open(path.get(), 'r') as f:
        if prefix_ == "":
            for line in f:
                words.append(line.strip('\n'))
        else:
            for line in f:
                temp = line.strip('\n')
                if temp.startswith(prefix_):
                    words.append(temp.strip(prefix_))

    bot = Bot()
    friends = list(map(lambda x: bot.friends().search(x.get())[0], friends))

    interval_time_val = int(interval_time.get())

    def send_news():
        for fri in friends:
            # fri.send('嘿嘿嘿')
            fri.send(words[random.randint(0, len(words)-1)])
        t = Timer(interval_time_val, send_news)
        t.start()

    send_news()


def stop_send():
    sys.exit(0)


tk.Button(window, text='添加发送好友', command=add_send_friend).place(x=10, y=100)

tk.Button(window, text='开始友好的问候', command=send_msg).place(x=120, y=100)

tk.Button(window, text='停止友好的问候', command=stop_send).place(x=250, y=100)

window.mainloop()

说明

  • 版本python3
  • 要装wxpy和tkinter两个模块,直接pip install wxpypip install tkinter,tkinter可能python3自带
  • 在界面上依次输入 间隔时间、词库开头分割符、选择词库文件
  • 点击 添加发送好友,输入微信好友昵称或者备注,可以点击多此,添加多个
  • 点击 开始友好的问候,扫码开始发送
  • 点击 停止友好的问候,停止发送
  • PS:扫码后用不了说明账号本身用不了网页版微信
  • PS:分隔符的问题:假如有一份sample.txt词库,里面的句子是以@开头的,那么输入的地方就输入@。如果没有分割符,就一行一个句子,输入的地方不输入就好了
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值