Skip to main content

Chat bot for vk.com

Project description

PYVKBOT

Python

tests&build LICENCE Version

CONTENT

About

  • Author: zeph1rr
  • Python version: >=3.10

Installation

pip install pyvkbot

Bot

Methods

init

Creating instance for future working

Params:

  • token: str - access token of your group
  • main_chat: int - [OPTIONAL] main chat for bot
  • group_id: int - [OPTIONAL] id of your group
  • logging: bool - [OPTIONAL. DEFAULT: True] if True - logging in stdout by loguru.logger

Example:

import os
from PyVkBot import Bot

token = os.getenv("TOKEN")

bot = Bot(token=token, main_chat=123, group_id=123123123, logging=False)

delete_message

Deleting message from group chat, if author is not admin

Params:

  • peer_id: int - id of chat where you want to delete messages
  • cmidis: list - list of conversations ids of message, which you want to delete

Example:

from PyVkBot import Bot

def delete_some_message(bot: Bot, message: dict):
    bot.delete_message(peer_id=message['peer_id'], cmids=[message['conversation_message_id']])

on

Creating handler for events in longpoll listen

Params:

  • event: str - Event type. You can take in from Types
  • callback: Callable - function, which is executed, when event triggers
  • trigger: str - trigger for event. Command for message or type of event for other (you can get it from Types)

Example:

from PyVkBot import Types

def test_handler(bot, message):
  print(bot, message)

# triggers when user left or kicked from chat
bot.on(Types.EventTypes.ACTION, test_handler, Types.ActionTypes.CHAT_KICK_USER)
# triggers when user sent audio message
bot.on(Types.EventTypes.ATTACHMENT, test_handler, Types.AttachmentTypes.AUDIO_MESSAGE)
# triggers when user sent "hello"
bot.on(Types.EventTypes.MESSAGE, test_handler, "hello")
# triggers when user sent any test message
bot.on(Types.EventTypes.MESSAGE, test_handler)

send_api_method

Sending some vk api method

Params:

  • method: str - vk_api method. See Docs for learn more
  • payload: dict - dict of data to send in method

Example

bot.send_api_method("messages.send", {"peer_id": 123, "text": "hi", "random_id":0})

send_message

Sending message in chat

Params:

  • peer_id: int - id of chat, where you want to send message
  • text: str - text of message you wanted to send
  • keyboard: Union[str, Keyboard] - [OPTIONAL] Keyboard for user, if needed. Takes Keyboard class or json string

Example:

from PyVkBot.Keyboard import Keyboard

# Sends hi message to chat
bot.send_message(peer_id=123, text='hi')

# Send hello message to chat with keyboard
keyboard = Keyboard(inline=False)
keyboard.add_button(label="123")
bot.send_message(peer_id=123, text="hello", keyboard=keyboard)
# or
bot.send_message(peer_id=123, text="hello", keyboard=keyboard.get_keyboard())

# Send hello message to chat with removing keyboard
keyboard=Keyboard(inline=False).get_empty_keyboard()
bot.send_message(peer_id=123, text="hello", keyboard=keyboard)

start

Creating handler for "start" button in private messages

Params:

  • callback: Callable - function, which is executed, when button is pushed

Example:

from PyVkBot import Bot

def start_command(bot: Bot, message: dict):
    bot.send_message(peer_id=message['peer_id'], text="Hello there!")

bot.start(start_command)

start_polling

Starting polling messages from users

Params:

  • callback: Callable - [OPTIONAL] Function, which would be executed, when bot is started

Example

bot.start_polling()
bot.start_polling(lambda *_: print("BOT STARTED!"))

Keyboard

Creating a vk keyboard

Params:

  • one_time: bool - [OPTIONAL. DEFAULT: False] If true, keyboard closes when user pushes the buttons
  • inline: bool - [OPTIONAL. DEFAULT: False] if ture, keyboard will be inline

Example:

from PyVkBot.Keyboard import Keyboard

# Simple vk keyboard
simple_keyboard = Keyboard()

# Simple one time vk keyboard
one_time_keyboard = Keyboard(one_time=True)

# Inline vk keyboard
inline_keyboard = Keyboard(inline=True)

All keyboard methods you can see in doc. Callback methods are not implemented.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

PyVkBot-1.0.4.tar.gz (5.5 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page