Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Commit

Permalink
yo!yo!yo!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
0x24a committed May 24, 2023
1 parent aa83e25 commit 10b4f6f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
53 changes: 36 additions & 17 deletions hiyobot/zhangchat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import websocket,ssl,json,threading,uuid,time,logging,re,traceback,inspect,asyncio
import websocket,ssl,json,threading,uuid,time,logging,re,traceback,inspect,asyncio,queue
from functools import wraps
HIYOBOT_VERSION=(0,2,4)
HIYOBOT_VERSION=(0,2,5)
MAX_RECV_LOG_LIMIT=100 #0 for no limit
def _isasync(func):
if inspect.isasyncgenfunction(func) or inspect.iscoroutinefunction(func):
Expand Down Expand Up @@ -112,7 +112,7 @@ def process(data):
for check in self.checks:
if check[0].match_all(data):
logging.debug(f"Matched!")
check[1](check[2],session)
check[1](Data(data),check[2],session)
self.checks.remove(check)
logging.debug(f"Skipping event matching because check has matched.")
continue
Expand Down Expand Up @@ -238,27 +238,46 @@ def __init__(self,bot,data):
self.extra_datas={}
self.created_on=time.time()
self.sessionID=str(uuid.uuid4())
def wait_for_input(self,prompt=None,expires=60,delay=100):
self.extra_datas["waitforinput"]=False
def wait_for_input(self,prompt=None,expires=60,checkrule=["chat","whisper"]):
"""
Wait for user input, timeout in 60s.
"""
if prompt:
self.bot.send(prompt)
#bind checker
nick=self.data["nick"]
checker=Matcher(Matchers.nick(nick))
def check(session,newsession):
checker=Matcher(lambda x:(x.get("cmd") == "chat" and x.get("nick") == nick) or (x.get("cmd") == "info" and x.get("type") == "whisper" and x.get("from") == nick) or (x.get("cmd") == "onlineRemove" and x.get("nick") == nick))
callbackq=queue.Queue()
def check(data,session,newsession):
#checked:
logging.debug(f"Callback running: {session} and new one {newsession}")
session.extra_datas["waitforinput"]=newsession.data.get("text")
if data.cmd == "onlineRemove":
logging.debug("Target left, kill session.")
callbackq.put([1]) #1=expire
return

#extract
if data.cmd == "chat" and "chat" in checkrule:
#public msg
content=data.text
elif data.cmd == "info" and "whisper" in checkrule:
#private msg
content=data.text.split(" 向你发送私信:",1)[1]
else:
#what the hell was that
raise NotImplementedError("Unknown message type")
callbackq.put([0,content,data])
self.bot.checks.append((checker,check,self))
stt=time.time()
while (time.time()-stt)<expires:
time.sleep(delay/1000)
if self.extra_datas["waitforinput"]:
logging.debug("Got message!")
return self.extra_datas["waitforinput"]
logging.debug("Session expired.")
self.extra_datas["waitforinput"]=False
raise TimeoutError("Session expired.")
try:
data=callbackq.get(timeout=expires)
if data[0] == 0:
return data[1],data[2]
if data[0] == 1:
#what the hell was thatagain?
raise NotImplementedError("Unknown callback status")
except:
logging.debug("Session expired.")
raise TimeoutError("Session expired.")

class Plugin:
def __init__(self):
Expand Down
7 changes: 7 additions & 0 deletions hiyobot/zhc-wait4inp.test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import zhangchat,os
bot=zhangchat.Bot("chat","hiyotest",os.getenv("zhc_password"))
@bot.on(zhangchat.Matcher(lambda x:x.get("text") == "awat"))
async def awat(session,data):
session.bot.send(str(session.wait_for_input("awa!")))

bot.run()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='hiyobot', # 你的项目名称
version="0.2.4", # 你的项目版本
version="0.2.5", # 你的项目版本
description='A simple bot framework for Hack.chat.', # 你的项目简介
long_description=open('README.md').read(), # 你的项目详细介绍,一般从README.md文件中读取
long_description_content_type='text/markdown', # 你的项目详细介绍的格式,一般为markdown格式
Expand Down

0 comments on commit 10b4f6f

Please sign in to comment.