commands = {"привет": "Здравствуй!", "пока": "До свидания!"}
def send_msg(peer_id: int, message: str, attachment: str = None):
return vk.method("messages.send", {**locals(), "random_id": 0})
def main():
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
send_msg(
event.obj.peer_id,
commands.get(event.obj.text.lower(), "Я не знаю такой команды")
)
if __name__ == "__main__":
main()
params = {'group_id': 98446009, "sort": "id_desc", "offset": i, "fields": "online, sex", 'access_token': good_token, 'v': '5.103'}
members = requests.post('https://api.vk.com/method/groups.getMembers', data=params, headers=headers)
print(members.json())
fields
должны быть перечислены через запятую def get_members(peer_id: int):
return vk.method("messages.getConversationMembers", {
"peer_id": peer_id
})
if event.object.text.lower() == "онлайн":
members = [
f"[id{i['id']}|{i['first_name']} {i['last_name']}]"
for i in get_members(event.object.peer_id)["profiles"]
if i["online"] == 1
]
vk.method("messages.send", {
"peer_id": event.obj.peer_id,
"random_id": 0,
"message": "Сейчас в сети: {}".format(
"\n".join(members)
)
})
def get_members(peer_id: int) -> list:
return vk.method("messages.getConversationMembers", {
"peer_id": peer_id
})["items"]
def get_admins(peer_id: int) -> list:
return [
i["member_id"] for i in get_members(peer_id)
if i.get("is_admin", False)
]
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
if event.text.lower() == "any_command":
if event.obj.from_id in get_admins(event.obj.peer_id):
do_something()
longpoll = VkBotLongPoll(vk_session, idgrup)
def get_user(pattern: str) -> int:
if "[id" in pattern:
return int(pattern.split("|")[0].replace("[id", ""))
if "vk.com/" in pattern:
domen = pattern.split("/")[-1]
return vk.users.get(user_ids=domen)[0]["id"]
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
text = event.obj["message"]["text"].split()
if (len(text) == 2) and text[0] == "кик":
try:
vk.messages.removeChatUser(
chat_id=event.chat_id,
member_id=get_user(text[1])
)
except Exception as e:
vk.messages.send(
chat_id=event.chat_id,
message="Произошла ошибка.",
random_id=0
)
text
и он становится строкой, а не списком.text = ','.join(text)
message_id
первый элемент строки text
.message_id = text[0]
text
у нас "13453, 23454", то message_id
будет 1
, потому что первый элемент строки - это 1.def message_cb(bot, event):
bot.send_text(chat_id=event.from_chat,
text="Hello with buttons.",
inline_keyboard_markup="{}".format(json.dumps([
[{"text": "Action 1", "callbackData": "call_back_id_1", "style": "attention"},
{"text": "Action 2", "callbackData": "call_back_id_2", "style": "attention"}],
[{"text": "Action 3", "callbackData": "call_back_id_2", "style": "primary"}
{"text": "Action 4", "callbackData", "call_back_id_2", "style": "primary"}]
])))
import discord
import re
from tinydb import TinyDB, Query
import asyncio
from discord.ext import commands
async def my_background_task():
await client.wait_until_ready()
while not client.is_closed:
with open("stats.txt", "a") as f:
f.write("Hello\n")
await asyncio.sleep(5) # task runs every 60 seconds
class MyClient(discord.Client):
async def on_ready(self):
print("Logged as {0}!".format(self.user))
def recognize(self, test_str):
regex = r"--(\w+)=([^--]+)"
matches = re.findall(regex, test_str)
return matches
async def on_message(self, message):
# print('699352136273887292' == str(message.guild.id))
# print('699352136273887292' + ' || ' + str(message.guild.id))
print("Message from {0.author}: {0.content}".format(message))
text = message.content.lower()
if "!разослать" in text and message.author.bot is False:
b = client.get_guild(699352136273887292)
users = []
for member in b.members:
user = client.get_user(member.id)
if user.bot:
continue
users.append(str(member.id))
print(users)
params = {'users': ','.join(users)}
recognize_objects = self.recognize(text)
for object in recognize_objects:
params[object[0].strip()] = object[1].strip()
db = TinyDB('db.json')
db.insert(params)
client = MyClient()
client.loop.create_task(my_background_task)
client.loop.run_forever()