-
Notifications
You must be signed in to change notification settings - Fork 0
/
book-keeping-bot.py
177 lines (147 loc) · 7.45 KB
/
book-keeping-bot.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# coding: utf-8
# 記帳\借款機器人
# Created by WilliamMou on 2017/8/17.
# In[1]:
import telepot
from telepot.loop import MessageLoop
from telepot.namedtuple import (
ReplyKeyboardMarkup,
KeyboardButton,
InlineKeyboardMarkup,
InlineKeyboardButton
)
from random import choice
import json
# In[2]:
#自行更新 #自行更新 #自行更新
TOKEN = 'bot token' #自行更新
#自行更新 #自行更新 #自行更新
# In[3]:
bot = telepot.Bot(TOKEN)
#資料結構:{chatid:[[+-,money,event],[+-,money,even],[+-,money,even]]}
moneydict={}
#資料傑{idtousername:{data:[1,]}} 0:未確認 1:確認 2:還款確認 pop:還款完成
lenddict={}
#列印訊息接收log
def print_msg(msg):
print(json.dumps(msg, indent=4))
#接收chat後執行:
def on_chat(msg):
#得取基礎資料:訊息類型\聊天室總類\聊天室id
header = telepot.glance(msg, flavor="chat")
print_msg(msg)
data=""
if header[0] == "text":
text = msg["text"]
# command
if text.startswith("/"):
command = text.lstrip("/")
if command == "start":
text = "OK, {}\n你準備好了...... 讓我們開始記帳吧"
bot.sendMessage(header[2], text.format(msg["from"]["first_name"]))
bot.sendMessage(header[2], "記帳請依序輸入 /add +/- <money> <porj>")
bot.sendMessage(header[2], "請輸入/list查看帳本,輸入/total 獲取總資產")
bot.sendMessage(header[2], "借款請依序輸入 /lend <@username> <money> <porj>")
bot.sendMessage(header[2], "查看借貸請輸入 /ldict <@username>")
elif command[:3] == "add":
#data=[+-,money,event]
data=command[3:].split()
if data[0] == '+' or data[0]=='-':
try:
int(data[1])
bot.sendMessage(header[2],"增加收支細項"+str(data[2]))
if header[2] in moneydict:
moneydict[header[2]].append(data)
else:
moneydict[header[2]]=[data]
bot.sendMessage(header[2],"收支帳本"+str(moneydict[header[2]]))
except:
bot.sendMessage(header[2],"請符合格式ouo")
else:
bot.sendMessage(header[2],"請符合格式ouo")
elif command[:4] == "lend":
data=command[4:].split()
bot.sendMessage(header[2],"借款給 "+str(data[0])+" "+str(data[1])+"元")
if msg["from"]["username"]+"to"+str(data[0][1:]) in lenddict:
lenddict[str(msg["from"]["username"])+"to"+str(data[0][1:])][msg["date"]]=[0,str(data[1])]
else:
lenddict[str(msg["from"]["username"])+"to"+str(data[0][1:])]={msg["date"]:[0,str(data[1])]}
bot.sendMessage(header[2],"請借款人 "+str(data[0])+" 回傳 /borrow @"+str(msg["from"]["username"])+" "+str(msg["date"])+" 驗證")
#bot.sendMessage(header[2],str(lenddict))
'''
if str(msg["from"]["username"])+"to"+str(data[0]) in lenddict:
lenddict[str(msg["from"]["username"])+"to"+str(data[0])]={msg[data]:0}
bot.sendMessage(header[2],0)
else:
lenddict[str(msg["from"]["username"])+"to"+str(data[0])]={msg[data]:0}
'''
#commond: /borrow @username date
elif command[:6] == "borrow":
data=command[6:].split()
lenddict[data[0][1:]+"to"+msg["from"]["username"]][int(data[1])][0]=1
#bot.sendMessage(header[2],str(lenddict))
bot.sendMessage(header[2],"提醒:輸入 /payback " + data[0] + " " +data[1]+" 還款")
#commond: /payback @username date
elif command[:7] == "payback":
data=command[7:].split()
lenddict[data[0][1:]+"to"+msg["from"]["username"]][int(data[1])][0]=2
bot.sendMessage(header[2],"請 "+data[0]+" 確認 @"+str(msg["from"]["username"])+" 是否還款,並輸入 /payok @"+msg["from"]["username"]+" "+str(data[1])+" 確認")
#commond: /payok @username date
elif command[:5] == "payok":
data=command[5:].split()
lenddict[msg["from"]["username"]+"to"+data[0][1:]].pop(int(data[1]))
bot.sendMessage(header[2],"還款確認完成><資料已核銷")
elif command == "list":
for i in range(len(moneydict[header[2]])):
bot.sendMessage(header[2],"收支帳本"+str(moneydict[header[2]][i]))
# /ldict @username
elif command[:5] == "ldict":
data=command[5:].split()
if msg["from"]["username"]+"to"+data[0][1:] in lenddict:
#for i in lenddict[msg["from"]["username"]+"to"+data[0][1:]]:
for i in lenddict[msg["from"]["username"]+"to"+data[0][1:]]:
if lenddict[msg["from"]["username"]+"to"+data[0][1:]][i][0]==0:
bot.sendMessage(header[2]," 欠款待確認,欠"+str(lenddict[msg["from"]["username"]+"to"+data[0][1:]][i][1])+"元")
elif lenddict[msg["from"]["username"]+"to"+data[0][1:]][i][0]==1:
bot.sendMessage(header[2]," 欠款確認,欠"+str(lenddict[msg["from"]["username"]+"to"+data[0][1:]][i][1])+"元")
elif lenddict[msg["from"]["username"]+"to"+data[0][1:]][i][0]==2:
bot.sendMessage(header[2]," 還款待確認,欠"+str(lenddict[msg["from"]["username"]+"to"+data[0][1:]][i][1])+"元")
else:
bot.sendMessage(header[2],str(data[0])+" 暫無欠您的款項")
elif command[:5] == "total":
s=0
for i in range(len(moneydict[msg['chat']['id']])):
if moneydict[msg['chat']['id']][i][0]=='+':
try:
s+=int(moneydict[msg['chat']['id']][i][1])
except:
pass
else:
try:
s-=int(moneydict[msg['chat']['id']][i][1])
except:
pass
bot.sendMessage(header[2],str(msg['chat']['id'])+"總資產:"+str(s))
# other msg
else:
# 我覺得不行!
image_url = "https://cdn.pixabay.com/photo/2016/03/22/23/45/money-1273908_960_720.jpg"
bot.sendPhoto(header[2], image_url)
bot.sendMessage(header[2],"輸入/start查看指令")
'''
# 回應按鈕
replyKeyboard = InlineKeyboardMarkup(inline_keyboard=[
[
InlineKeyboardButton(
text="在一個穩定的發揮後,卻得到一個這樣的反饋。我覺得有點失控",
callback_data="test"
)
]
])
bot.sendMessage(header[2], text="選擇回應", reply_markup=replyKeyboard)
'''
MessageLoop(bot, {
'chat': on_chat,
#'callback_query': on_callback_query,
}).run_as_thread()
print('Listening ...')