python實(shí)現(xiàn)定時發(fā)送qq消息
因?yàn)樯钪欣鲜峭浉鞣N事情,剛好又在學(xué)python,便突發(fā)奇想通過python實(shí)現(xiàn)提醒任務(wù)的功能(盡管TIM有定時功能),也可定時給好友、群、討論組發(fā)送qq消息。其工作流程是:訪問數(shù)據(jù)庫提取最近計(jì)劃——>根據(jù)數(shù)據(jù)內(nèi)容(提醒時間、提醒對象、提醒內(nèi)容)設(shè)置定時任務(wù)——>給特定qq好友發(fā)送消息。
1. 軟件版本:

2.安裝依賴環(huán)境
- pymysql安裝:pip install pymysql
- qqbot安裝:pip install qqbot
3.數(shù)據(jù)庫操作
數(shù)據(jù)庫操作非常簡單,跟Java類似,自己去菜鳥教程看一下基礎(chǔ)語法就好了。
#coding: utf-8
import pymysql #導(dǎo)入pymysql模塊
db = pymysql.connect("localhost","root","root","info_db" ) #數(shù)據(jù)庫鏈接信息
cursor = db.cursor()
#插入任務(wù)
def insertSchedule(schedule):
insertsql = "insert into dutyschedule_tb(worktime,name) values(%s,%s)"
try:
#這種查詢語句可以防止sql注入
cursor.execute(insertsql,(schedule['worktime'],schedule['name']))
db.commit()
except Exception:
db.rollback()
raise Exception
#刪除任務(wù)
def deleteSchedule():
deletesql = ""
try:
cursor.execute(deletesql)
db.commit()
except Exception:
db.rollback()
def updateSchedule(user):
updatesql = ""
try:
cursor.execute(updatesql)
db.commit()
except Exception:
db.rollback()
#獲取下一個任務(wù)
def findScheduleByNewTime():
selectsql = "SELECT * FROM dutyschedule_tb where NOW() <= date_format(worktime,'%Y-%m-%d %H:%i:%S') ORDER BY worktime ASC;"
try:
cursor.execute(selectsql)
results = cursor.fetchone()
schedule = {}
schedule['worktime'] = results[1]
schedule['name'] = results[2]
schedule['content'] = results[3]
return schedule
except Exception:
return None
4.配置qqbot登陸信息
也可以不配置,不配置的話就是每次掃碼登陸,但這在Linux系統(tǒng)下不好用,我按說明將配置改成了每次將登陸二維碼發(fā)到固定qq郵箱。qqbot模塊在GitHub上,大家可以去看一下模塊說明:qqbot
配置文件默認(rèn)在用戶目錄下的.qqbot-tmp/v2.3.conf,linux下類似
{
# QQBot 的配置文件
# 使用 qqbot -u somebody 啟動程序時,依次加載:
# 根配置 -> 默認(rèn)配置 -> 用戶 somebody 的配置 -> 命令行參數(shù)配置
# 使用 qqbot 啟動程序時,依次加載:
# 根配置 -> 默認(rèn)配置 -> 命令行參數(shù)配置
"fantasy" : {
# 這是自己創(chuàng)建的用戶自定義配置,可以在啟動qqbot啟動時選擇加載哪個配置文件
# QQBot-term (HTTP-API) 服務(wù)器端口號(該服務(wù)器監(jiān)聽 IP 為 127.0.0.1 )
# 設(shè)置為 0 則不會開啟本服務(wù)器(此時 qq 命令和 HTTP-API 接口都無法使用)。
"termServerPort" : 8188,
# 二維碼 http 服務(wù)器 ip,請?jiān)O(shè)置為公網(wǎng) ip 或空字符串
"httpServerIP" : "",
# 二維碼 http 服務(wù)器端口號
"httpServerPort" : 8189,
# 自動登錄的 QQ 號
"qq" : "你的qq",
# 接收二維碼圖片的郵箱賬號
"mailAccount" : "你的郵箱",
# 該郵箱的 IMAP/SMTP 服務(wù)授權(quán)碼,一般在郵箱設(shè)置中有
"mailAuthCode" : "你的授權(quán)碼",
# 是否以文本模式顯示二維碼
"cmdQrcode" : False,
# 顯示/關(guān)閉調(diào)試信息
"debug" : False,
# QQBot 掉線后自動重啟
"restartOnOffline" : True,
# 在后臺運(yùn)行 qqbot ( daemon 模式)
"daemon": False,
# 完成全部聯(lián)系人列表獲取之后才啟動 QQBot
"startAfterFetch" : False,
# 插件目錄
"pluginPath" : ".",
# 啟動時需加載的插件
"plugins" : [],
# 插件的配置(由用戶自定義)
"pluginsConf" : {},
},
# 用戶 somebody 的配置,這是默認(rèn)配置
"somebody" : {
#這里的基本內(nèi)容跟上面一樣,就不貼出來了,太長了占地方
},
# 可以在 默認(rèn)配置 中配置所有用戶都通用的設(shè)置
"默認(rèn)配置" : {
"qq" : "",
"pluginPath" : "",
"plugins" : [
'qqbot.plugins.sampleslots',
'qqbot.plugins.schedrestart',
],
"pluginsConf" : {
'qqbot.plugins.schedrestart': '8:00',
}
},
# # 注意:根配置是固定的,用戶無法修改(在本文件中修改根配置不會生效)
# "根配置" : {
# "termServerPort" : 8188,
# "httpServerIP" : "",
# "httpServerPort" : 8189,
# "qq" : "",
# "mailAccount" : "",
# "mailAuthCode" : "",
# "cmdQrcode" : False,
# "debug" : False,
# "restartOnOffline" : False,
# "daemon" : False,
# "startAfterFetch" : False,
# "pluginPath" : "",
# "plugins" : [],
# "pluginsConf" : {}
# },
}
5. 自定義功能
from qqbot import _bot as bot
#登陸qq,使用配置文件為fantasy
bot.Login(['-u','fantasy'])
#自定義函數(shù),用來進(jìn)一步封裝qqbot接口
#獲取所有好友列表
def getBuddyByName(nickname):
return bot.List('buddy',nickname)
#獲取所有群列表
def getGroupByName(groupname):
return bot.List('group',groupname)
#給備注(沒用備注就是昵稱)為nickname的好友發(fā)送content消息
def sendToNickname(nickname,content):
user = getBuddyByName(nickname)
if user:
bot.SendTo(user[0],content)
else:
print("未找到聯(lián)系人:"+nickname)
6.入口主程序
#coding: utf-8
import time
import sched
import datetime
from Dao.DutyscheduleDao import *
from Utils.QQInterface import *
#sched是python的定時任務(wù)模塊
schedule = sched.scheduler(time.time, time.sleep)
#從數(shù)據(jù)庫獲取第一個任務(wù)
newschedule = findScheduleByNewTime()
#返回距下次任務(wù)還有多少秒
def getSeconds():
#申明全局變量
global newschedule
newschedule = findScheduleByNewTime()
if newschedule:
return (newschedule['worktime'] - datetime.datetime.now()).total_seconds()
else:
print("所有任務(wù)執(zhí)行完畢,退出程序……")
exit()
#發(fā)消息函數(shù)
def SendTo():
global newschedule
sendToNickname(newschedule['name'],newschedule['content'])
#中間函數(shù),用于循環(huán)運(yùn)行所有數(shù)據(jù)庫未執(zhí)行事件
def perform():
SendTo()
#睡眠5秒,不然可能會重復(fù)發(fā)送消息
time.sleep(5)
sleepSecond = getSeconds()
print("下次任務(wù)執(zhí)行時間:"+str(newschedule['worktime']))
#這次任務(wù)執(zhí)行完后添加新任務(wù)
schedule.enter(sleepSecond,1,perform,())
def run():
#1.獲取數(shù)據(jù)庫最近將執(zhí)行任務(wù)的時間及姓名
#2.計(jì)算執(zhí)行任務(wù)的時間與現(xiàn)在時間的差值(單位:秒)
sleepSecond = getSeconds()
print("下次通知:"+str(newschedule['worktime']))
#3.加入定時處理函數(shù)
schedule.enter(sleepSecond,1,perform,())
#4.執(zhí)行定時任務(wù)
schedule.run()
if __name__ == '__main__':
run()
7.其它
數(shù)據(jù)庫結(jié)構(gòu):
drop database if exists info_db; create database info_db default character set utf8; use info_db; create table dutyschedule_tb( id int(11) auto_increment primary key, worktime timestamp not null, name varchar(10) not null, content varchar(100) not null )engine=InnoDB auto_increment=1 default charset=utf8;
以上就是循環(huán)發(fā)送qq消息的代碼,以下是項(xiàng)目目錄結(jié)構(gòu),其中一些沒有出現(xiàn)的文件是自己測試所用,可以不用關(guān)心:

效果圖:


總結(jié):基本功能完成了,但是操作不夠友好,需要手動往數(shù)據(jù)庫錄入數(shù)據(jù),之后準(zhǔn)備做一個數(shù)據(jù)管理的前端配合使用,可以簡化很多操作。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python中openpyxl和xlsxwriter對Excel的操作方法
這篇文章主要介紹了python中openpyxl和xlsxwriter對Excel的操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
Python 實(shí)現(xiàn) WebSocket 通信的過程詳解
WebSocket是一種在Web應(yīng)用程序中實(shí)現(xiàn)雙向通信的協(xié)議,與傳統(tǒng)的HTTP請求-響應(yīng)模型不同,WebSocket允許服務(wù)器主動向客戶端推送數(shù)據(jù),實(shí)現(xiàn)實(shí)時性和互動性,這篇文章主要介紹了Python 實(shí)現(xiàn) WebSocket 通信的過程詳解,需要的朋友可以參考下2024-06-06
python檢查目錄文件權(quán)限并修改目錄文件權(quán)限的操作
這篇文章主要介紹了python檢查目錄文件權(quán)限并修改目錄文件權(quán)限的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
python3+selenium獲取頁面加載的所有靜態(tài)資源文件鏈接操作
這篇文章主要介紹了python3+selenium獲取頁面加載的所有靜態(tài)資源文件鏈接操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05

