Skip to content

Commit

Permalink
Update readme and fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovizro committed Jan 1, 2024
1 parent 9ee8e8e commit cf71d93
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,12 @@ bot = Bot(

@karuha.on(MessageEvent)
async def reply(event: MessageEvent) -> None:
bot = event.bot
if event.text == "Hello!":
await event.bot.publish(event.topic, "Hello world!")


if __name__ == "__main__":
karuha.load_config()
karuha.init_config()
karuha.add_bot(bot)
karuha.run()
```


8 changes: 2 additions & 6 deletions README_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,11 @@ bot = Bot(
@karuha.on(MessageEvent)
async def reply(event: MessageEvent) -> None:
if event.text == "Hello!":
PublishEvent.new(
event.bot,
event.topic,
"Hello world!"
)
await event.bot.publish(event.topic, "Hello world!")


if __name__ == "__main__":
karuha.load_config()
karuha.init_config()
karuha.add_bot(bot)
karuha.run()
```
15 changes: 7 additions & 8 deletions karuha/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ async def async_run(self, server_config: Optional[ServerConfig] = None) -> None:
while self.state == State.running:
self.logger.info(f"starting the bot {self.name}")
try:
async with self._run_context() as channel:
async with self._run_context(server) as channel:
stream = get_stream(channel) # type: ignore
msg_gen = self._message_generator()
client = stream(msg_gen)
Expand Down Expand Up @@ -460,11 +460,10 @@ def _prepare_loop_task(self) -> None:
self.queue = Queue()
self._loop_task_ref = ref(asyncio.current_task())

def _get_channel(self) -> grpc_aio.Channel: # pragma: no cover
assert self.server
host = self.server.host
secure = self.server.ssl
ssl_host = self.server.ssl_host
def _get_channel(self, server_config: ServerConfig, /) -> grpc_aio.Channel: # pragma: no cover
host = server_config.host
secure = server_config.ssl
ssl_host = server_config.ssl_host
if not secure:
self.logger.info(f"connecting to server at {host}")
return grpc_aio.insecure_channel(host)
Expand All @@ -473,8 +472,8 @@ def _get_channel(self) -> grpc_aio.Channel: # pragma: no cover
return grpc_aio.secure_channel(host, grpc.ssl_channel_credentials(), opts)

@asynccontextmanager
async def _run_context(self) -> AsyncGenerator[grpc_aio.Channel, None]: # pragma: no cover
channel = self._get_channel()
async def _run_context(self, server_config: ServerConfig, /) -> AsyncGenerator[grpc_aio.Channel, None]: # pragma: no cover
channel = self._get_channel(server_config)
try:
yield channel
except: # noqa: E722
Expand Down

0 comments on commit cf71d93

Please sign in to comment.