forked from AlexR1712/openvebot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
124 lines (99 loc) · 4.55 KB
/
bot.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Simple Bot to get OpenVe Telegram Links
from uuid import uuid4
import re
import random
import requests
import sys
from bs4 import BeautifulSoup
from telegram import InlineQueryResultArticle, ParseMode, InlineKeyboardMarkup, InlineKeyboardButton, \
InputTextMessageContent
from telegram.ext import Updater, InlineQueryHandler, CommandHandler
import logging
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
# Token obtenido de @botfather en Telegram
TOKEN = sys.argv[1] # Let it die token needs to be specified.
# Define a few command handlers. These usually take the two arguments bot and
# update. Error handlers also receive the raised TelegramError object in error.
def start(bot, update):
update.message.reply_text(
'Hola! soy un bot que permite buscar entre los grupos que se encuentran '+
'en la OpenVe, para usarme solo debes escribir en cualquier chat @OpenVebot y colocar el nombre del grupo a buscar.')
def help(bot, update):
update.message.reply_text('Si tienes problemas con el bot, contacta con @alexr1712 mi creador! o al equipo de openve via https://t.me/openve')
def escape_markdown(text):
"""Helper function to escape telegram markup symbols"""
escape_chars = '\*_`\['
return re.sub(r'([%s])' % escape_chars, r'\\\1', text)
def inlinequery(bot, update):
r = requests.get('https://github.com/OpenVE/comunidades-en-telegram/blob/master/README.md')
soup = BeautifulSoup(r.text, "html.parser")
results = list()
query = update.inline_query.query
communities = soup.find_all('tr')[2:]
if len(query) == 0:
random.shuffle(communities)
for tr in communities[:50]:
td = tr.find_all('td')
results.append(InlineQueryResultArticle(
id=uuid4(),
title=escape_markdown(td[0].text),
input_message_content=InputTextMessageContent(
"Nombre: {}\nLink: {} \nAdmins: {}".format(escape_markdown(td[0].text),td[2].text,td[1].text)),
url=td[2].text,
reply_markup=InlineKeyboardMarkup([
[InlineKeyboardButton('▶️ Ir al Grupo ◀️', url=td[2].text)],
[InlineKeyboardButton('🔎 Encontrar Grupos', switch_inline_query_current_chat="")]
])))
elif len(query) > 0:
found = 0
for tr in communities:
tds = tr.find_all('td')
if query.lower() in (tds[0].text).lower():
found+=1
results.append(InlineQueryResultArticle(
id=uuid4(),
title=tds[0].text,
input_message_content=InputTextMessageContent(
"Nombre: {}\nLink: {} \nAdmins: {}".format(escape_markdown(tds[0].text),tds[2].text,tds[1].text)),
url=tds[2].text,
reply_markup=InlineKeyboardMarkup([
[InlineKeyboardButton('▶️ Ir al Grupo ◀️', url=tds[2].text)],
[InlineKeyboardButton('🔎 Encontrar Grupos', switch_inline_query_current_chat="")]
])))
if found == 0:
results.append(InlineQueryResultArticle(
id=uuid4(),
title='No existen items con ese termino'))
else:
results.append(InlineQueryResultArticle(
id=uuid4(),
title='No existen items'))
update.inline_query.answer(results)
def error(bot, update, error):
logger.warn('Update "%s" caused error "%s"' % (update, error))
def main():
# Create the Updater and pass it your bot's token.
updater = Updater(TOKEN)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
# on noncommand i.e message - echo the message on Telegram
dp.add_handler(InlineQueryHandler(inlinequery))
# log all errors
dp.add_error_handler(error)
# Start the Bot
updater.start_polling()
# Block until the user presses Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()