This repository has been archived by the owner on Sep 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
90 lines (71 loc) · 2.22 KB
/
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
#!/usr/bin/env python3
import sys
import time
import json
from pprint import pprint
import telepot
from telepot.loop import MessageLoop
from tasks.sample_task import SampleTask
from tasks.remind_task import RemindTask
from tasks.weather_task import WeatherTask
from tasks.restaurant_task import RestaurantTask
from tasks.viewcoupon_task import ViewCouponTask
from tasks.subscribe_task import SubscribeTask
from modules.sample_module import SampleModule
# from modules.remind_module import RemindModule
from modules.coupon_module import CouponModule
# 載入設定值
with open(sys.path[0] + '/config.json', 'r') as f:
config = json.load(f)
# 取得 bot 控制權
bot = telepot.Bot(config['BOT_TOKEN'])
# 存放使用者的資料
users = {}
# 功能與模組間的全域變數
data = {}
# 載入功能
tasks = [SampleTask(bot), WeatherTask(bot), RestaurantTask(bot, config['PLACE_KEY']), SubscribeTask(bot, data), RemindTask(bot), ViewCouponTask(bot, data)]
# 載入模組
modules = [SampleModule(bot), CouponModule(bot,data)]
def on_chat(msg):
# 除錯訊息
pprint(msg)
# 取得使用者的資訊
content_type, chat_type, chat_id = telepot.glance(msg)
# 有新的使用者就新增到 users 裡
if not chat_id in users:
users[chat_id] = {
'status': None
}
# 嘗試觸發每個功能
for task in tasks:
if task.trig(users, msg):
task.main(users, msg)
break
def on_callback_query(msg):
# 除錯訊息
pprint(msg)
# 取得使用者的 from_id
query_id, chat_id, query_data = telepot.glance(msg, flavor='callback_query')
# 嘗試觸發每個功能
for task in tasks:
if task.trig(users, msg):
task.main(users, msg)
break
def main():
# 接收使用者對 bot 的輸入並以相關函式處理
MessageLoop(bot, {
'chat': on_chat,
'callback_query': on_callback_query
}).run_as_thread()
print("我開始運作啦!")
# 執行模組的 setup()
for module in modules:
module.setup()
# 每隔 10 秒觸發模組的 loop()
while True:
for module in modules:
module.loop()
time.sleep(10)
if __name__ == '__main__':
main()