欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實現(xiàn)定時發(fā)送qq消息

 更新時間:2019年01月18日 10:28:22   作者:丿風色幻想  
這篇文章主要為大家詳細介紹了python實現(xiàn)定時發(fā)送qq消息,具有一定的參考價值,感興趣的小伙伴們可以參考一下

因為生活中老是忘記各種事情,剛好又在學python,便突發(fā)奇想通過python實現(xiàn)提醒任務的功能(盡管TIM有定時功能),也可定時給好友、群、討論組發(fā)送qq消息。其工作流程是:訪問數(shù)據(jù)庫提取最近計劃——>根據(jù)數(shù)據(jù)內(nèi)容(提醒時間、提醒對象、提醒內(nèi)容)設置定時任務——>給特定qq好友發(fā)送消息。

1. 軟件版本:

2.安裝依賴環(huán)境

  • pymysql安裝:pip install pymysql
  • qqbot安裝:pip install qqbot

3.數(shù)據(jù)庫操作

數(shù)據(jù)庫操作非常簡單,跟Java類似,自己去菜鳥教程看一下基礎語法就好了。

#coding: utf-8
import pymysql #導入pymysql模塊

db = pymysql.connect("localhost","root","root","info_db" ) #數(shù)據(jù)庫鏈接信息
cursor = db.cursor()

#插入任務
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

#刪除任務
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()

#獲取下一個任務
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
配置文件默認在用戶目錄下的.qqbot-tmp/v2.3.conf,linux下類似

{

 # QQBot 的配置文件
 # 使用 qqbot -u somebody 啟動程序時,依次加載:
 # 根配置 -> 默認配置 -> 用戶 somebody 的配置 -> 命令行參數(shù)配置
 # 使用 qqbot 啟動程序時,依次加載:
 # 根配置 -> 默認配置 -> 命令行參數(shù)配置
 
 "fantasy" : {
  # 這是自己創(chuàng)建的用戶自定義配置,可以在啟動qqbot啟動時選擇加載哪個配置文件 
 # QQBot-term (HTTP-API) 服務器端口號(該服務器監(jiān)聽 IP 為 127.0.0.1 )
 # 設置為 0 則不會開啟本服務器(此時 qq 命令和 HTTP-API 接口都無法使用)。
 "termServerPort" : 8188,
 
 # 二維碼 http 服務器 ip,請設置為公網(wǎng) ip 或空字符串
 "httpServerIP" : "",
 
 # 二維碼 http 服務器端口號
 "httpServerPort" : 8189,
 
 # 自動登錄的 QQ 號
 "qq" : "你的qq",
 
 # 接收二維碼圖片的郵箱賬號
 "mailAccount" : "你的郵箱",
 
 # 該郵箱的 IMAP/SMTP 服務授權碼,一般在郵箱設置中有
 "mailAuthCode" : "你的授權碼",
 
 # 是否以文本模式顯示二維碼
 "cmdQrcode" : False,
 
 # 顯示/關閉調(diào)試信息
 "debug" : False,

 # QQBot 掉線后自動重啟
 "restartOnOffline" : True,
 
 # 在后臺運行 qqbot ( daemon 模式)
 "daemon": False,
 
 # 完成全部聯(lián)系人列表獲取之后才啟動 QQBot 
 "startAfterFetch" : False,
 
 # 插件目錄
 "pluginPath" : ".",
 
 # 啟動時需加載的插件
 "plugins" : [],
 
 # 插件的配置(由用戶自定義)
 "pluginsConf" : {},
 
 },
 
 # 用戶 somebody 的配置,這是默認配置
 "somebody" : {
 #這里的基本內(nèi)容跟上面一樣,就不貼出來了,太長了占地方
 
 
 },
 
 # 可以在 默認配置 中配置所有用戶都通用的設置
 "默認配置" : {
 "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ù),用來進一步封裝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的定時任務模塊


schedule = sched.scheduler(time.time, time.sleep)
#從數(shù)據(jù)庫獲取第一個任務
newschedule = findScheduleByNewTime()

#返回距下次任務還有多少秒
def getSeconds():
 #申明全局變量
 global newschedule
 newschedule = findScheduleByNewTime()
 if newschedule:
 return (newschedule['worktime'] - datetime.datetime.now()).total_seconds()
 else:
 print("所有任務執(zhí)行完畢,退出程序……")
 exit()

#發(fā)消息函數(shù)
def SendTo():
 global newschedule
 sendToNickname(newschedule['name'],newschedule['content'])

#中間函數(shù),用于循環(huán)運行所有數(shù)據(jù)庫未執(zhí)行事件
def perform():
 SendTo()
 #睡眠5秒,不然可能會重復發(fā)送消息
 time.sleep(5)
 sleepSecond = getSeconds()
 print("下次任務執(zhí)行時間:"+str(newschedule['worktime']))
 #這次任務執(zhí)行完后添加新任務
 schedule.enter(sleepSecond,1,perform,())


def run():
 #1.獲取數(shù)據(jù)庫最近將執(zhí)行任務的時間及姓名
 #2.計算執(zhí)行任務的時間與現(xiàn)在時間的差值(單位:秒)
 sleepSecond = getSeconds()
 print("下次通知:"+str(newschedule['worktime']))
 #3.加入定時處理函數(shù)
 schedule.enter(sleepSecond,1,perform,())
 #4.執(zhí)行定時任務
 schedule.run()

if __name__ == '__main__':
 run()

7.其它

數(shù)據(jù)庫結構:

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àn)的文件是自己測試所用,可以不用關心:

在這里插入圖片描述

效果圖:

在這里插入圖片描述

總結:基本功能完成了,但是操作不夠友好,需要手動往數(shù)據(jù)庫錄入數(shù)據(jù),之后準備做一個數(shù)據(jù)管理的前端配合使用,可以簡化很多操作。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論