forked from spmall/simpleweixinservicer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcIf4weixin.py
141 lines (118 loc) · 4.63 KB
/
funcIf4weixin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# -*- coding: UTF-8 -*-
import xml.etree.ElementTree as ET
import timeHelper
RESPONSE_TEXT_TEMPLATE = '''
<xml>
<ToUserName><![CDATA[{TO_USER}]]></ToUserName>
<FromUserName><![CDATA[{FROM_USER}]]></FromUserName>
<CreateTime>{TIME_STEMP}</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[{RESPONSE_CONTENT}]]></Content>
</xml>
'''
class msgHandler:
def __init__(self, data):
self.data = data
self.dict = self._xmlToDict(self.data)
if self.dict['MsgType'] == 'event':
self.worker = eventHandler(self.dict['FromUserName'],self.dict['Event'])
else:
self.worker = txtmsgHandler(self.dict['FromUserName'],self.dict['Content'])
def response(self):
responseDict = self.responseDict()
text = self.responseXML(responseDict)
return text
def _xmlToDict(self, xmlText):
xmlDict = {}
itemlist = ET.fromstring(xmlText)
for child in itemlist:
xmlDict[child.tag] = child.text
print xmlDict
return xmlDict
def responseXML(self, dataDict):
if dataDict:
text = RESPONSE_TEXT_TEMPLATE
for key, value in dataDict.items():
parameter = '{%s}' % key
text = text.replace(parameter, value)
print text
else:
text = ''
return text
def responseDict(self):
responseDict = {}
try:
responseDict['RESPONSE_CONTENT'] = self.worker.response.encode('UTF-8')
responseDict['TO_USER'] = self.dict['FromUserName']
responseDict['FROM_USER'] = self.dict['ToUserName']
responseDict['TIME_STEMP'] = str(timeHelper.unixTimeStamp())
except:
pass
return responseDict
class eventHandler:
MSG_WELCOME = u'欢迎您关注我,想了解我,就请发送“帮助”或“?”'
def __init__(self, user, event):
if event == 'subscribe':
self.response = self.MSG_WELCOME
class txtmsgHandler:
MSG_HELP = u'''我会记录您对我说的除“查询”和“帮助”以外的所有的话,“查询”会得到您最后一次对我说的话
'''
MSG_SUCCESS = [u'存储完成', u'我存好了,随时来查哦',u'搞定,收工']
def __init__(self, user, reqMsg):
self.req = reqMsg.lower()
self.db = simpledb(user)
self.response = self.MSG_HELP
self._handle_req()
def _handle_req(self):
if self.req in ['help', '帮助', '?', u'?']: return
if self.req in ['cx', '查询']: return self.chaxun()
else: return self.jilu()
def chaxun(self):
timepoint, content = self.db.chaxun()
self.response = u'您在{}说过:{}'.format(timepoint, content)
pass
def jilu(self):
self.db.jilu(self.req)
self.response = self._get_success_response()
def _get_success_response(self):
import random
return self.MSG_SUCCESS[random.randint(0,len(self.MSG_SUCCESS)-1)]
import os
import sqlite3
import timeHelper
class simpledb:
def __init__(self, dbName):
dbPath = os.path.join(os.getcwd(), 'DB')
if not os.path.isdir(dbPath):
os.mkdir(dbPath)
name = os.path.join(dbPath, dbName+'.db')
print name
createNeeded = False
if not os.path.isfile(name):
print "First Time Store, Create DB"
createNeeded = True
self.conn = sqlite3.connect(name)
self.c = self.conn.cursor()
if createNeeded:
self._create_db()
def __del__(self):
#本来想实现数据库文件可以手动删除的,但是因为sqlite的bug,close之后文件也没关闭,所以这个代码就浪费了。理想是好的,仍然保留在这里
self.conn.close()
def _create_db(self):
self.c.execute('''CREATE TABLE yulu
(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, timestamp INTEGER, content text)''')
self.c.execute('''CREATE INDEX dateIndex ON yulu(timestamp)''')
self.conn.commit()
def jilu(self, content):
timestamp = timeHelper.unixTimeStamp()
self.c.execute('''INSERT INTO yulu (timestamp, content) VALUES({},'{}')'''.format(timestamp,content))
self.conn.commit()
def chaxun(self):
self.c.execute('''SELECT * from yulu ORDER BY id DESC LIMIT 1''')
try:
id,timestamp,content = self.c.fetchone()
timepoint = timeHelper.timestamp2datetime(timestamp)
except:
timepoint = '-'
content = '-'
return timepoint, content