This repository has been archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
control_dispatch.py
executable file
·79 lines (52 loc) · 2.63 KB
/
control_dispatch.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
from handler.base_plugin import CommandPlugin
from utils import upload_photo
import asyncio, aiohttp, io
class DispatchPlugin(CommandPlugin):
def __init__(self, *commands, prefixes=None, strict=False):
"""Allows admins to send out messages to users."""
if not commands:
commands = ("разослать",)
super().__init__(*commands, prefixes=prefixes, strict=strict)
async def process_message(self, msg):
if not msg.meta["is_admin_or_moder"]:
return await msg.answer("🤜🏻 У вас недостаточно прав.")
cmd_len = len(msg.meta.get("__prefix", "")) + len(msg.meta.get("__command", ""))
message = msg.full_text[cmd_len:].strip()
attachment = ""
for a in await msg.get_full_attaches():
if a.type != "photo":
attachment += str(a) + ","
if a.type == "photo" and a.url:
async with aiohttp.ClientSession() as sess:
async with sess.get(a.url) as resp:
new_a = await upload_photo(self.api, io.BytesIO(await resp.read()))
if not new_a:
continue
attachment += str(new_a) + ","
await msg.answer("> Приступаю к рассылке!")
if await self.dispatch(message, attachment) is False:
return await msg.answer("< Ошибка при отправлении! Попробуйте позже!")
return await msg.answer("< Рассылка закончена!")
async def dispatch(self, message, attachment):
dialogs = await self.bot.api.messages.getDialogs(count=1, preview_length=1)
if not dialogs or "count" not in dialogs:
return False
dialogs = dialogs["count"]
users = set()
tasks = []
for i in range(int(dialogs / 200) + 1):
tasks.append(await self.bot.api(wait="custom").messages.getDialogs(count=200, preview_length=1))
await asyncio.wait_for(
asyncio.gather(*tasks, return_exceptions=True), None)
for r in tasks:
if not r or not r.result():
continue
r = r.result()
for dialog in r.get("items", []):
if "message" not in dialog or "user_id" not in dialog["message"]:
continue
users.add(int(dialog["message"]["user_id"]))
for i, u in enumerate(users):
await self.bot.api(wait="no").messages.send(user_id=u, message=message, attachment=attachment)
if i % 25 == 0:
await asyncio.sleep(0.2)