深圳全飞鸿
标题:
itchat_qty.py的源码
[打印本页]
作者:
zhgc
时间:
2019-7-7 23:39
标题:
itchat_qty.py的源码
#coding=utf8
import MySQLdb,itchat,time,importlib,sys,threading
from threading import Timer
#reload(sys)
importlib.reload(sys)
#sys.setdefaultencoding('utf8')
db_config = {
'host': '192.168.2.10',
'user': 'root',
'passwd': 'xxxxx',
'db': 'crc',
'charset': 'utf8'
}
db1=MySQLdb.connect(**db_config)
db2=MySQLdb.connect(**db_config)
db3=MySQLdb.connect(**db_config)
#db.ping(True)
itchat.auto_login(hotReload=True)
users = itchat.search_chatrooms(name='凤天办公室')
username=users[0]['UserName']
# 这里是我们在“1. 实现微信消息的获取”中已经用到过的同样的注册方法,isGroupChat=True
@itchat.msg_register(itchat.content.TEXT,isGroupChat=True)
#实时回复任务
def reply(msg):
if(msg.isAt):
try:
db1.ping()
except MySQLdb.Error as e:
#这是防止db连接离线 报错为(2006, 'MySQL server has gone away')
if (e and (e.args[0]==2006 or e.args[0]==2013 )):
db1.ping(True)
#print 'ping(true) connect'
else:
#print "timer_MySQL Error : %s" % e
print("e")
try:
cursor1 = db1.cursor()
rooms=msg['User']['NickName']
fromuser_name=msg['ActualNickName']
mess=msg['Text']
to_rooms_id = msg['User']['UserName']
x='@x'
cursor1.callproc('CRC_IN',(rooms,fromuser_name,mess,x))
data1= cursor1.fetchone()
mo_data=data1[0]
mess_data ="来自群[%s]\r发送人[%s]\r消息内容:%s \r回复消息:%s"%(rooms,fromuser_name,mess,mo_data)
print(mess_data)
#回复群消息
itchat.send(msg=mo_data, toUserName=to_rooms_id)
#转发群消息至工作群
itchat.send(msg=mess_data, toUserName=username)
cursor1.close()
db1.commit()
except MySQLdb.Error as e:
print("itchat_rooms_MySQL Error : %s" % e)
#itchat.send(msg="MySQL Error %d: %s" % ( e.args[0], e.args[1] ), toUserName=username)
#itchat.auto_login(hotReload=True)
#users = itchat.search_chatrooms(name='凤天办公室')
#username=users[0]['UserName']
#线程定时器执行函数
def auto_run():
try:
db2.ping()
except MySQLdb.Error as e:
if (e and (e.args[0]==2006 or e.args[0]==2013 )):
db2.ping(True)
else:
print("timer_MySQL Error : %s" % e)
try:
cursor2=db2.cursor()
cursor2.execute("SELECT now()")
data ="DB时间:%s" %cursor2.fetchone()
cursor2.close()
itchat.send(msg=data, toUserName=None)
itchat.send(msg=data, toUserName=username)
print('这次定时已经跑完 mess:%s'%data)
t = Timer(28800, auto_run)
t.start()
db2.commit()
except MySQLdb.Error as e:
if (e and (e.args[0]==2006 or e.args[0]==2013 )):
db2.ping(True)
else:
print("timer_MySQL Error : %s" % e)
#itchat.auto_login(hotReload=True)
#users = itchat.search_chatrooms(name='凤天办公室')
#username=users[0]['UserName']
#线程循环查看系统有无信息需要回复
def thread_run():
cursor3 = db3.cursor()
sql = "SELECT mess_id,chatrooms,fromusername,out_mess FROM r_message WHERE UPPER(send_flag) ='N' AND UPPER(EVENT)='Y' AND out_mess IS NOT NULL"
while(True):
try:
#ab=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
time.sleep(2)
cursor3.execute(sql)
# 获取所有记录列表
results = cursor3.fetchall()
#print 'count:',len(results)
for row in results:
back_mess="任务已经处理\r\n任务单号:%s\r\n创建人员:%s\r\n回复信息:%s" % \
(row[0],row[2],row[3])
users_id1 = itchat.search_chatrooms(name=row[1])
usergroupname1=users_id1[0]['UserName']
itchat.send(msg=back_mess, toUserName=usergroupname1)
#转发群消息至工作群
itchat.send(msg=back_mess, toUserName=username)
print(back_mess)
sql_up="UPDATE r_message SET send_flag='Y',out_time=now() WHERE mess_id='%s'" % row[0]
cursor3.execute(sql_up)
db3.commit()
db3.commit()
except MySQLdb.Error as e:
if (e and (e.args[0]==2006 or e.args[0]==2013 )):
db3.ping(True)
else:
print("timer_MySQL Error : %s" %e)
#itchat.auto_login(hotReload=True)
#users = itchat.search_chatrooms(name='凤天办公室')
#username=users[0]['UserName']
cursor3.close()
if __name__ == "__main__":
ab=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
print("start_time:",ab)
auto_run()
t =threading.Thread(target=thread_run)
t.setDaemon(True)#设置线程为后台线程
t.start()
itchat.send(msg=ab, toUserName=None)
itchat.run()
#db.close()
复制代码
作者:
zhgc
时间:
2020-12-27 16:40
全飞鸿CRC系统v3版 2019-12-02
用时间比较长的版本
欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/)
Powered by Discuz! X3.2