def main(): """ Пример создания клавиатуры для отправки ботом """ vk_session = vk_api.VkApi(token='bot_api_token') vk = vk_session.get_api() keyboard = VkKeyboard(one_time=True) keyboard.add_button('Белая кнопка', color=VkKeyboardColor.DEFAULT) keyboard.add_button('Зелёная кнопка', color=VkKeyboardColor.POSITIVE) keyboard.add_line() # Переход на вторую строку keyboard.add_location_button() keyboard.add_line() keyboard.add_vkpay_button(hash="action=transfer-to-group&group_id=74030368&aid=6222115") keyboard.add_line() keyboard.add_vkapps_button(app_id=6979558, owner_id=-181108510, label="Отправить клавиатуру", hash="sendKeyboard") vk.messages.send( peer_id=123456, random_id=get_random_id(), keyboard=keyboard.get_keyboard(), message='Пример клавиатуры' )
def create_payment_buttons(amount, recipient=PAYEE): keyboard = VkKeyboard() keyboard.add_button("Наличными", payload=json.dumps("cash")) keyboard.add_line() keyboard.add_button( "Банковской картой онлайн", payload=json.dumps("bank_card"), color=VkKeyboardColor.POSITIVE, ) keyboard.add_line() pay_hash = ( f"action=pay-to-user&aid={VK_GRROUP_ID}&amount={amount}&user_id={recipient}" ) keyboard.add_vkpay_button(hash=pay_hash, payload=json.dumps("vk_pay")) return keyboard.get_keyboard()
def start_keyboard(uid, event, vk): keyboard = VkKeyboard(one_time=False) keyboard.add_button('Стандартные заметки', color=VkKeyboardColor.POSITIVE, payload={'key': 'notes'}) keyboard.add_line() keyboard.add_vkpay_button(hash='action=transfer-to-user&user_id=127949564', payload={'key': 'donate'}) vk.messages.send(user_id=uid, message='Управляйте ботом с помощью кнопок', keyboard=keyboard.get_keyboard(), random_id=get_random_id())
'19ab16e3bc6d67d71f88137cd2a0f50588adad0720966c436813134074cffedab541e11f3a3d6cd42a2e0' ) from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType longpoll = VkBotLongPoll(vk_session, '203967942') vk = vk_session.get_api() from vk_api.longpoll import VkLongPoll, VkEventType Lslongpoll = VkLongPoll(vk_session) Lsvk = vk_session.get_api() keyboard = VkKeyboard(one_time=True) keyboard.add_button('Привет', color=VkKeyboardColor.NEGATIVE) keyboard.add_button('Клавиатура', color=VkKeyboardColor.POSITIVE) keyboard.add_line() keyboard.add_location_button() keyboard.add_line() keyboard.add_vkpay_button(hash="action=transfer-to-group&group_id=203967942") def getText(text): start = text.find('text') + 8 end = text.find("'", start) return text[start:end] for event in longpoll.listen(): if event.type == VkBotEventType.MESSAGE_NEW: vars = ['Да', 'да', 'дА', 'ДА'] varss = ['Соси', 'соси'] if getText(str(event)).lower() == "да": if event.from_chat: vk.messages.send(
try: # vk_session.auth(token_only=True) vk = vk_session.get_api() long_poll = VkBotLongPoll(vk_session, 194736647) keyboard = VkKeyboard(one_time=True) keyboard.add_button("Кнопка 1", color=VkKeyboardColor.DEFAULT) keyboard.add_button("Кнопка 2", color=VkKeyboardColor.NEGATIVE) keyboard.add_button("Кнопка 3.1", color=VkKeyboardColor.POSITIVE) keyboard.add_line() keyboard.add_button("Кнопка 4", color=VkKeyboardColor.PRIMARY) keyboard.add_line() keyboard.add_location_button() keyboard.add_line() keyboard.add_vkpay_button(hash="action=pay-to-user&amount=100&user_id=23382988") keyboard.add_line() keyboard.add_vkpay_button(hash="action=transfer-to-group&group_id=194736647&aid=7439933") for event in long_poll.listen(): if event.type == VkBotEventType.MESSAGE_NEW and event.obj['message']['from_id']: print(event) print(str(event.obj['message']['from_id']) + " " + event.obj['message']['text']) vk.messages.send(user_id=event.obj['message']['from_id'], message="Привет", random_id=random.randint(1, 10000), keyboard=keyboard.get_keyboard()) else: print(event) # friends = vk.friends.get() # print(friends) #
'ff8895cc1ea1152cc791777bba683265a18454c45269abecc7d78e9cafef96f906e380bded43e0a610aa0' ) from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType longpoll = VkBotLongPoll(vk_session, 201836241) vk = vk_session.get_api() from vk_api.longpoll import VkLongPoll, VkEventType Lslongpoll = VkLongPoll(vk_session) Lsvk = vk_session.get_api() keyboard = VkKeyboard(one_time=True) keyboard.add_button('Ассортимент', color=VkKeyboardColor.NEGATIVE) keyboard.add_button('Список городов', color=VkKeyboardColor.POSITIVE) keyboard.add_line() keyboard.add_location_button() keyboard.add_line() keyboard.add_vkpay_button(hash="action=transfer-to-group&group_id=201836241") ''' for event in longpoll.listen(): if event.type == VkBotEventType.MESSAGE_NEW: if 'Ку' in str(event) or 'Привет' in str(event) or 'Хай' in str(event) or 'Хелло' in str(event) or 'Хеллоу' in str(event): if event.from_chat: vk.messages.send( key = ('147ea3c3df9321785d11125a53653fb815454107'), #ВСТАВИТЬ ПАРАМЕТРЫ server = ('https://lp.vk.com/wh201836241'), ts=('4'), random_id = get_random_id(), message='Привет!', chat_id = event.chat_id ) if 'Клавиатура' in str(event): if event.from_chat:
from vk_api.utils import get_random_id vk_session = vk_api.VkApi(token='') from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType longpoll = VkBotLongPoll(vk_session, '') vk = vk_session.get_api() from vk_api.longpoll import VkLongPoll, VkEventType Lslongpoll = VkLongPoll(vk_session) Lsvk = vk_session.get_api() keyboard = VkKeyboard(one_time=True) keyboard.add_button('Привет', color=VkKeyboardColor.NEGATIVE) keyboard.add_button('Клавиатура', color=VkKeyboardColor.POSITIVE) keyboard.add_line() keyboard.add_location_button() keyboard.add_line() keyboard.add_vkpay_button( hash="action=transfer-to-group&group_id=еще_раз_ID_группы") for event in longpoll.listen(): if event.type == VkBotEventType.MESSAGE_NEW: if 'Ку' in str(event) or 'Привет' in str(event) or 'Хай' in str( event) or 'Хелло' in str(event) or 'Хеллоу' in str(event): if event.from_chat: vk.messages.send( key=(''), #ВСТАВИТЬ ПАРАМЕТРЫ server=(''), ts=(''), random_id=get_random_id(), message='Привет!', chat_id=event.chat_id) if 'Клавиатура' in str(event): if event.from_chat:
import vk_api, random from vk_api.longpoll import VkLongPoll, VkEventType from vk_api.keyboard import VkKeyboard, VkKeyboardColor # API токен сообщества mytoken = '' keyboard = VkKeyboard(one_time=True, inline=True) keyboard.add_button('Привет', color=VkKeyboardColor.NEGATIVE) keyboard.add_button('Клавиатура', color=VkKeyboardColor.POSITIVE) keyboard.add_line() keyboard.add_location_button() keyboard.add_line() keyboard.add_vkpay_button(hash="action=transfer-to-group&group_id=198461809") # Функция посылающая сообщение def write_msg(user_id, message): random_id = vk_api.utils.get_random_id() vk.method('messages.send', {'user_id': user_id, 'message': message, 'random_id': random_id}) def create_keyboard(response): keyboard = VkKeyboard(one_time=True) if response == 'привет': keyboard.add_button('Хочу тян', color=VkKeyboardColor.POSITIVE) keyboard.add_button('Тян не нужны!', color=VkKeyboardColor.NEGATIVE) elif response == 'какой у меня выбор?': keyboard.add_button('Хочу тян', color=VkKeyboardColor.POSITIVE)
def new_keyboard(dicts, perm=True): from vk_api.keyboard import VkKeyboard, VkKeyboardColor if perm == False: keyboard = VkKeyboard(one_time=True) elif perm == True: keyboard = VkKeyboard(one_time=False) def color(title, col): col = str(col).upper() m = [['POSITIVE', '3', 'ЗЕЛЕНЫЙ'], ['NEGATIVE', '2', 'КРАСНЫЙ'], ['SECONDARY', '1', 'БЕЛЫЙ'], ['PRIMARY', '0', 'СИНИЙ']] if col in m[0]: keyboard.add_button(title, color=VkKeyboardColor.NEGATIVE) elif col in m[1]: keyboard.add_button(title, color=VkKeyboardColor.POSITIVE) elif col in m[2]: keyboard.add_button(title, color=VkKeyboardColor.SECONDARY) elif col in m[3]: keyboard.add_button(title, color=VkKeyboardColor.PRIMARY) else: raise ValueError( 'Ошибка создания клавиатуры (new_keyboard):\n=====\nНеправильно указан цвет/указан специальный объект клавиатуры\n=====' ) for dt in dicts: try: for i in dt.keys(): if i == 'new_line': keyboard.add_line() elif i == 'vk_pay': keyboard.add_vkpay_button(hash=dt[i]) elif i == 'open_app': for t in range(4): dti = dt[i][t] for key, value in dti.items(): if key == 'app_id': app_id = value elif key == 'owner_id': owner_id = value elif key == 'label': label = value elif key == 'hash': hash1 = value keyboard.add_vkapps_button(app_id, owner_id, label, hash1) elif i == 'open_link': for t in range(2): dti = dt[i][t] for key, value in dti.items(): if key == 'label': label = value elif key == 'link': link = value keyboard.add_openlink_button(label, link) else: color(i, dt[i]) except: raise ValueError( 'Ошибка создания клавиатуры (new_keyboard):\n=====\nНеправильно указан один из параметров клавиатуры\n=====' ) return keyboard
def main(): vk_session = vk_api.VkApi(token='') vk = vk_session.get_api() longpoll = VkBotLongPoll(vk_session, 203559408) keyboard = VkKeyboard(one_time=True) keyboard.add_button('Комплимент', color=VkKeyboardColor.POSITIVE) keyboard.add_button('Милая картиночка', color=VkKeyboardColor.POSITIVE) keyboard.add_line() keyboard.add_button('Какой сегодня праздник', color=VkKeyboardColor.POSITIVE) keyboard.add_line() keyboard.add_vkpay_button( hash="action=transfer-to-group&group_id=203559408") for event in longpoll.listen(): if event.type == VkBotEventType.MESSAGE_NEW: vk.messages.send(user_id=event.obj.message['from_id'], random_id=get_random_id(), message=':)', keyboard=keyboard.get_keyboard()) if event.from_user: if 'Ку' in str(event) or 'Привет' in str( event) or 'Хай' in str(event) or 'Хелло' in str( event) or 'Хеллоу' in str(event): if event.from_user: vk.messages.send(user_id=event.obj.message['from_id'], message='Привет, милый человек :3', random_id=get_random_id()) elif 'Комплимент' in str(event): if event.from_user: vk.messages.send(user_id=event.obj.message['from_id'], random_id=get_random_id(), keyboard=keyboard.get_keyboard(), message=compl[random.randint( 0, len(compl) - 1)]) elif 'Милая картиночка' in str(event): if event.from_user: vk.messages.send(user_id=event.obj.message['from_id'], random_id=get_random_id(), keyboard=keyboard.get_keyboard(), message='Попробую отправить.....') upload = vk_api.VkUpload(vk) photo = upload.photo_messages( f'{random.randint(1, 6)}.jpg') owner_id = photo[0]['owner_id'] photo_id = photo[0]['id'] access_key = photo[0]['access_key'] attachment = f'photo{owner_id}_{photo_id}_{access_key}' vk.messages.send(peer_id=event.object.peer_id, user_id=event.obj.message['from_id'], random_id=get_random_id(), attachment=attachment) vk.messages.send(user_id=event.obj.message['from_id'], random_id=get_random_id(), keyboard=keyboard.get_keyboard(), message='Получилось!!! Я молодец ^-^') elif 'Какой сегодня праздник' in str(event): current_date = date.today() month = current_date.month day = current_date.day if month < 10: data = str(day) + '.0' + str(month) else: data = str(day) + '.' + str(month) with open( "C:/Users/Polina/PycharmProjects/the_best_bot_vk/holidays", encoding='utf-8') as f: for line in f.readlines(): if data == line.split()[0]: mes = ' '.join(line.split()[1:]) break f.close() if event.from_user: vk.messages.send(user_id=event.obj.message['from_id'], random_id=get_random_id(), keyboard=keyboard.get_keyboard(), message=mes) if event.from_user: vk.messages.send(user_id=event.obj.message['from_id'], random_id=get_random_id(), keyboard=keyboard.get_keyboard(), message='Поздравляю!!!!') else: vk.messages.send(user_id=event.obj.message['from_id'], message="простите, я вас не понимаю :(((", random_id=get_random_id())
import data from datetime import datetime from vk_api.utils import get_random_id token = '135735d4d2a12308a9b0233246f5c3573ffa41f744116df49c7eacd15828ec3830a9d09ca7a10b3657b21' vk_session = vk_api.VkApi(token=token) session_api = vk_session.get_api() longpoll = VkLongPoll(vk_session) keyboard = VkKeyboard(one_time=True) keyboard.add_button('Привет', color=VkKeyboardColor.NEGATIVE) keyboard.add_button('Клавиатура', color=VkKeyboardColor.POSITIVE) keyboard.add_line() keyboard.add_location_button() keyboard.add_line() keyboard.add_vkpay_button(hash="action=transfer-to-group&group_id=183415444") while True: for event in longpoll.listen(): if event.type == VkEventType.MESSAGE_NEW: print('сообщение пришло в: ' + str(datetime.strftime(datetime.now(), "%H:%M:%S"))) print('Текст сообщения: ' + str(event.text)) print(event.user_id) response = event.text.lower() if event.from_user and not (event.from_me): if response == "привет": vk_session.method( 'messages.send', { 'user_id': event.user_id, 'message': 'Привет, как ты понимаешь - я родился!',
temp = w.temperature('celsius') tempa = temp['temp'] if tempa>0: tempa = m.ceil(tempa) if tempa<0: tempa = m.floor(tempa) tempa = str(tempa) WeatherMessage = 'Погода в городе Тверь сейчас ' + tempa + '℃' keyboard = VkKeyboard(one_time=True) keyboard.add_button('Айла, какая сегодня погода?', color=VkKeyboardColor.NEGATIVE) keyboard.add_button('Клавиатура', color=VkKeyboardColor.POSITIVE) keyboard.add_line() keyboard.add_location_button() keyboard.add_line() keyboard.add_vkpay_button(hash="action=transfer-to-group&group_id=200606276") current_datetime = datetime.now() hour = current_datetime.hour minute = current_datetime.minute print("Айла проснулась") if (hour >= 4) and (hour < 12): print('Доброе утро') if (hour >=12) and (hour < 18): print('Добрый день') if (hour >= 18) and (hour < 24): print('Добрый вечер') if (hour >= 1) and (hour < 4): print('Доброй ночи')