forked from MakDon/we-work-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
146 lines (110 loc) · 3.9 KB
/
example.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
from weworkbot import bot_mgr as bots
from weworkbot import BotMgr
from weworkbot import Bot as wBot
url = ""
# ============ hello world ====================
def hello_world():
wBot(url).set_text("hello world").send()
# ============ hello world ====================
def hello_world_twice():
bot = wBot(url)
bot.set_text('hello world').send()
bot.set_text('<font color="info">Hello world</font>', type='markdown').send()
# ================= run ========================
def run_forever():
# 每隔 60 秒发送一次 hello world
# 使用Bot.every() 设置间隔
wBot(url).set_text("hello world").run()
# ============== 定时提醒 =======================
def foo1():
wBot(url).set_text("every 30 seconds").every(30).run()
# ============== 带提醒人的定时提醒 ===============
def foo2():
wBot(url)\
.set_text("every 30 seconds")\
.set_mentioned_list(["wangqing", "@all"])\
.set_mentioned_mobile_list(["13800001111", "@all"])\
.every(30)\
.run()
# ============== 带条件的定时提醒 ================
def check_something(arg1, arg2, arg3=True):
return True
def foo3():
wBot(url)\
.set_text("every 30 seconds with condition")\
.check(check_something, args=['arg1', 'arg2'], kwargs={'arg3': 'arg3'})\
.every(30)\
.run()
# ============== 使用函数返回消息内容 =============
def render_text(arg1, arg2, arg3=True):
return arg1 + arg2 + arg3
def foo4():
# 当同时调用了 render_text 与 set_text 时,优先调用 render_text
# type 选项: 'text', 'markdown', 默认为 text
wBot(url)\
.render_text(render_text, args=['render ', 'with '], kwargs={'arg3': 'function'}, type='text')\
.check(check_something, args=['arg1', 'arg2'], kwargs={'arg3': 'arg3'})\
.every(30)\
.run()
# ============== 带计数器的定时提醒 =======================
def foo5():
# 在调用 check 5 次,或发送 3 次后停止运行
wBot(url)\
.set_check_counter(5)\
.set_send_counter(3) \
.check(check_something, args=['arg1', 'arg2'], kwargs={'arg3': 'arg3'})\
.set_text("every 30 seconds")\
.every(30)\
.run()
# ============== 使用多个 bot,或同一个 bot url 多种信息 ==================
def foo6():
# 在调用 check 5 次,或发送 3 次后停止运行
bots.add_bot(url)\
.set_check_counter(5)\
.set_send_counter(3) \
.check(check_something, args=['arg1', 'arg2'], kwargs={'arg3': 'arg3'})\
.set_text("every 30 seconds")\
.every(30)
# 在调用 check 6 次,或发送 5 次后停止运行
bots.add_bot(url) \
.set_check_counter(6) \
.set_send_counter(5) \
.check(check_something, args=['arg1', 'arg2'], kwargs={'arg3': 'arg3'}) \
.set_text("every 10 minutes") \
.every(minute=10)
bots.run()
# ============== 或创建多组 bot ==================
def foo7():
bots1 = BotMgr()
bots2 = BotMgr()
bots3 = BotMgr()
bot1 = wBot(url)\
.set_check_counter(5)\
.set_send_counter(3) \
.check(check_something, ['arg1', 'arg2'], {'arg3': 'arg3'})\
.set_text("every 30 seconds")\
.every(30)
bot2 = wBot(url) \
.set_check_counter(6) \
.set_send_counter(5) \
.check(check_something, ['arg1', 'arg2'], {'arg3': 'arg3'}) \
.set_text("every 10 minutes") \
.every(minute=10)
bot3 = wBot(url) \
.set_check_counter(6) \
.set_send_counter(5) \
.check(check_something, ['arg1', 'arg2'], {'arg3': 'arg3'}) \
.set_text("every 10 minutes") \
.every(hour=1)
bots1.append(bot1)
bots1.append(bot2)
bots2.append(bot2)
bots2.append(bot3)
bots3.append(bot1)
bots3.append(bot3)
bots1.start()
bots2.start()
bots3.start()
bots1.join()
bots2.join()
bots3.join()