-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
96 lines (82 loc) · 4.3 KB
/
bot.py
File metadata and controls
96 lines (82 loc) · 4.3 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
import requests## импорт библиотеки запросов
from bs4 import BeautifulSoup as bs## импорт библиотеки для разбора
from scr import *
first = 0
second = 0
def found(b,a): ## фунция поиска
URL_TEMPLATE = b + a ##переменная запроса
URL_TEMPLATE = URL_TEMPLATE.replace(" ", "") #очистка от лишних пробелов
r = requests.get(URL_TEMPLATE) #переменная запроса с сервера
soup = bs(r.text, "html.parser") #переменная анализа информации
wikiinfo = soup.find('div', class_="post") #поиск информации и задача переменной
data = wikiinfo.text
if len(data) > 4093:
data = (data[0:4093] + '...')
return data
def translateen(b,a):
URL_TEMPLATE = b + a ##переменная запроса
URL_TEMPLATE = URL_TEMPLATE.replace(" ", "") #очистка от лишних пробелов
r = requests.get(URL_TEMPLATE) #переменная запроса с сервера
soup = bs(r.text, "html.parser") #переменная анализа информации
wikiinfo = soup.find('div', class_="ru_title") #поиск информации и задача переменной
if data == None:
data = "Ничего не найдено по вашему запросу"
data = wikiinfo.text
if len(data) > 4093:
data = (data[0:4093] + '...')
return data
def translatefr(b,a):
URL_TEMPLATE = b + a ##переменная запроса
URL_TEMPLATE = URL_TEMPLATE.replace(" ", "") #очистка от лишних пробелов
r = requests.get(URL_TEMPLATE) #переменная запроса с сервера
soup = bs(r.text, "html.parser") #переменная анализа информации
wikiinfo = soup.find("p",class_="truncate") #поиск информации и задача переменной
if data == None:
data = "Ничего не найдено по вашему запросу"
data = wikiinfo.text
data = data[2:]
if len(data) > 4093:
data = (data[0:4093] + '...')
return data
def write_msg(user_id, message): #функция отправки сообщения
print(message)
vk.method('messages.send', {'user_id': user_id, 'message': message,"random_id":vk_api.utils.get_random_id()})
# Авторизуемся как сообщество
vk = vk_api.VkApi(token=token)
# Работа с сообщениями
longpoll = VkLongPoll(vk)
# Основной цикл
for event in longpoll.listen():
# Если пришло новое сообщение
if event.type == VkEventType.MESSAGE_NEW:
# Если оно имеет метку для меня( то есть бота)
if event.to_me:
# Сообщение от пользователя
request = event.text
if request[0] == "!":
request = request[1:]
if request.find("+") == 1:
first = int(request[request.find("+") + 1])
second = int(request[request.find("+") + 1:])
write_msg(event.user_id,first+second)
if request.find("+") == 1:
first = int(request[request.find("-") + 1])
second = int(request[request.find("-") + 1:])
write_msg(event.user_id,first-second)
if request.find("+") == 1:
first = int(request[request.find("*") + 1])
second = int(request[request.find("*") + 1:])
write_msg(event.user_id,first*second)
else:
first = int(request[request.find(":") + 1])
second = int(request[request.find(":") + 1:])
write_msg(event.user_id,first/second)
else:
if request == "стоп":
break
request = request.lower()
write_msg(event.user_id,translateen(SlovarEN,request))
write_msg(event.user_id,translatefr(SlovarFR,request))
write_msg(event.user_id,found(SlovarRU,request))