-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
77 lines (64 loc) · 2.45 KB
/
main.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
import telebot
import wikipedia
import re
from config.myconfig import TOKEN_BOT
bot = telebot.TeleBot(TOKEN_BOT)
@bot.message_handler(commands=["start", "help"])
def start_bot(message):
cid = message.chat.id
text = message.text
if text == "/start":
bot.send_message(cid, "/enwiki - English wikipedia\n/ruwiki - Русская википедия")
elif text == "/help":
pass
@bot.message_handler(commands=["enwiki", "ruwiki"])
def wiki_language(message):
cid = message.chat.id
text = message.text
if text == "/enwiki":
wikipedia.set_lang("en")
msg = bot.send_message(cid, "Enter any word to search Wikipedia")
bot.register_next_step_handler(msg, enwiki_search)
elif text == "/ruwiki":
wikipedia.set_lang("ru")
msg = bot.send_message(cid, "Введите любое слово для поиска в Wikipedia")
bot.register_next_step_handler(msg, ruwiki_search)
def enwiki_search(message):
cid = message.chat.id
text = message.text
try:
wiki_res = wikipedia.page(text).content[:1000]
wiki_sentences = wiki_res.split(".")[:-1]
wiki_result = ""
for sentence in wiki_sentences:
if not("==" in sentence):
if len(sentence.strip()) > 3:
wiki_result += sentence + "."
else:
break
wiki_result = re.sub(r"\([^()]*\)", "", wiki_result)
wiki_result = re.sub(r"\{[^\{\}]*\}", "", wiki_result)
bot.send_message(cid, wiki_result, parse_mode="HTML")
except Exception as e:
print(e)
bot.send_message(cid, "We can't find this")
def ruwiki_search(message):
cid = message.chat.id
text = message.text
try:
wiki_res = wikipedia.page(text).content[:1000]
wiki_sentences = wiki_res.split(".")[:-1]
wiki_result = ""
for sentence in wiki_sentences:
if not("==" in sentence):
if len(sentence.strip()) > 3:
wiki_result += sentence + "."
else:
break
wiki_result = re.sub(r"\([^()]*\)", "", wiki_result)
wiki_result = re.sub(r"\{[^\{\}]*\}", "", wiki_result)
bot.send_message(cid, wiki_result, parse_mode="HTML")
except Exception as e:
print(e)
bot.send_message(cid, "Не могу найти этот запрос")
bot.polling(none_stop=True, interval=0)