Skip to content

Commit

Permalink
created ClonesBot
Browse files Browse the repository at this point in the history
  • Loading branch information
kesha1225 committed Apr 9, 2020
1 parent e3915f6 commit 205f523
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 18 deletions.
24 changes: 24 additions & 0 deletions examples/bots/clones_bot_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
create many bots with same functionality
"""

from vkwave.bots.easy import GroupBot, TaskManager, ClonesBot


bot = GroupBot(tokens=["Bot0TOKEN"], group_id=444,)


@bot.message_handler(bot.text_filter("123"))
async def simple(event: bot.SimpleEvent):
await event.answer("HELLO")


clones = ClonesBot(bot, GroupBot("Bot1TOKEN", 192868628), GroupBot("Bot2TOKEN", 172702125))

clones.run_all_bots()


# or
# task_manager = TaskManager()
# task_manager.add_task(bot.run)
# task_manager.run()
4 changes: 2 additions & 2 deletions examples/bots/hello_world_bot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from vkwave.bots.easy import SimpleLongPollBot
from vkwave.bots.easy import GroupBot

bot = SimpleLongPollBot(tokens="MyToken", group_id=123456789)
bot = GroupBot(tokens="MyToken", group_id=123456789)


@bot.message_handler()
Expand Down
4 changes: 2 additions & 2 deletions examples/bots/simple_bot_example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from vkwave.bots.easy import SimpleLongPollBot, TaskManager
from vkwave.bots.easy import GroupBot, TaskManager


bot = SimpleLongPollBot(tokens="MyToken", group_id=123456789)
bot = GroupBot(tokens="MyToken", group_id=123456789)

# or if you want do a lot of requests without 'to many requests' errors
# bot = SimpleLongPollBot(tokens=["MyToken1", "MyToken2", "MyToken3"], group_id=123456789)
Expand Down
4 changes: 2 additions & 2 deletions examples/bots/vkscript_example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from vkwave.api.methods import APIOptionsRequestContext
from vkwave.bots.easy import SimpleLongPollBot, TaskManager
from vkwave.bots.easy import GroupBot, TaskManager
from vkwave.vkscript import execute
from vkwave.types.responses import ExecuteResponse

bot = SimpleLongPollBot(
bot = GroupBot(
tokens=["123"],
group_id=456,
)
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

```python
from vkwave.bots.easy.easy_bot import SimpleLongPollBot
from vkwave.bots.easy.easy_bot import GroupBot

bot = SimpleLongPollBot(tokens="MyToken", group_id=123456789)

Expand Down
2 changes: 1 addition & 1 deletion vkwave/bots/easy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .easy_bot import SimpleLongPollBot
from .easy_bot import GroupBot, ClonesBot
from .task_manager import TaskManager
37 changes: 27 additions & 10 deletions vkwave/bots/easy/easy_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from vkwave.bots.core.tokens.types import GroupId
from vkwave.client.default import AIOHTTPClient
from vkwave.longpoll.bot import BotLongpoll, BotLongpollData
from vkwave.types.bot_events import BotEventType, BaseBotEvent
from vkwave.types.bot_events import BotEventType
from vkwave.types.objects import BaseBoolInt


Expand Down Expand Up @@ -88,14 +88,10 @@ async def answer(
)


class SimpleLongPollBot:
class GroupBot:
def __init__(
self,
tokens: typing.Union[str, typing.List[str]],
group_id: int,
loop: asyncio.AbstractEventLoop = None,
self, tokens: typing.Union[str, typing.List[str]], group_id: int,
):
self.loop = asyncio.get_event_loop() or loop
self.SimpleEvent = SimpleEvent
self.api_session = create_api_session_aiohttp(tokens)
self.api_context = self.api_session.api.get_context()
Expand Down Expand Up @@ -138,6 +134,27 @@ async def run(self):
await self.dispatcher.cache_potential_tokens()
await self._lp.start()

def run_forever(self):
self.loop.create_task(self.run())
self.loop.run_forever()
def run_forever(self, loop: asyncio.AbstractEventLoop = None):
loop = loop or asyncio.get_event_loop()
loop.create_task(self.run())
loop.run_forever()


class ClonesBot:
"""
Create many bots with same functionality
"""

def __init__(self, base_bot: GroupBot, *clones: GroupBot):
self.base_bot = base_bot
self.router = self.base_bot.router
self.clones: typing.Tuple[GroupBot] = clones

def run_all_bots(self, loop: asyncio.AbstractEventLoop = None):
loop = loop or asyncio.get_event_loop()
loop.create_task(self.base_bot.run())
for clone in self.clones:
clone.router.registrar.handlers.extend(self.router.registrar.handlers)
clone.router.registrar.handlers = list(set(clone.router.registrar.handlers))
loop.create_task(clone.run())
loop.run_forever()

0 comments on commit 205f523

Please sign in to comment.