Created
May 4, 2020 06:46
-
-
Save yinian1992/ab38ff85ab81ba25370f615f9052aef1 to your computer and use it in GitHub Desktop.
Weixin Bot Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import werobot | |
import random | |
robot = werobot.WeRoBot(token='zzz', | |
enable_session=True) | |
@robot.handler | |
def echo(message, session): | |
if message.content == '猜数字': | |
digits = [str(i) for i in range(10)] | |
random.shuffle(digits) | |
session[message.source] = (digits[:4], 0, 0, 8) | |
return '游戏开始,数字为四位,请回复数字。' | |
if message.source in session: | |
user = session[message.source] | |
if message.content == '查看答案': | |
return '答案:' + ''.join(user[0]) | |
if len(message.content) == 4 and message.content.isdigit(): | |
a = 0 | |
b = 0 | |
for i in range(4): | |
if user[0][i] in message.content: | |
if user[0][i] == message.content[i]: | |
a += 1 | |
else: | |
b += 1 | |
user[1] = a | |
user[2] = b | |
user[3] -= 1 | |
if user[3] == 0: | |
del(session[message.source]) | |
return '结果:{0}A{1}B,答案为:{2},游戏结束!'.format( | |
user[1], user[2], user[0]) | |
if user[1] == 4: | |
del(session[message.source]) | |
return '结果:4A0B,恭喜你!' | |
return '结果:{0}A{1}B,剩余{2}次。'.format( | |
user[1], user[2], user[3]) | |
return '回复「猜数字」开始游戏。' | |
robot.run(port=23300) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment