-
Notifications
You must be signed in to change notification settings - Fork 0
/
onfesas_bot.py
129 lines (102 loc) · 4.87 KB
/
onfesas_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
125
126
127
128
129
import logging
from telegram.ext import Updater, CommandHandler, ConversationHandler, MessageHandler, Filters
from telegram import ReplyKeyboardMarkup, KeyboardButton
from parser import news, rss_news
updater = Updater(token='1041164852:AAFrXEoznar1FRMxuELTtgHrY-Ltv6N_SBs', use_context=True)
dispatcher = updater.dispatcher
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
ADD_CATEGORY, ADD_KEYWORD, ADD_RSS, RSS_KEYWORD = range(4)
dictionary = {'Science🎆': 'Наука и техника', 'Economy💰': 'Экономика', 'Internet📲': 'Интернет и СМИ',
'Culture🕌': 'Культура', 'Travelling🏝': 'Путешествия', 'Life🏞': 'Из жизни',
'Sport🏆': 'Спорт', 'World🌎': 'Мир', 'Russia🇷🇺': 'Россия'}
def start(update, context):
context.bot.send_message(chat_id=update.effective_chat.id,
text="Hi Dear ✋ \nI'm News Bot 👨💻 \nI can show you all the latest news 📚\n"
"What sphere are you interested in❓\nType <category> and choose one 📝\nOr <rss> to add your RSS 📲")
def choose_category(update, context):
bot = context.bot
keyboard = [
[KeyboardButton("Science🎆")],
[KeyboardButton("Economy💰")],
[KeyboardButton("Internet📲")],
[KeyboardButton("Culture🕌")],
[KeyboardButton("Travelling🏝")],
[KeyboardButton("Life🏞")],
[KeyboardButton("Sport🏆")],
[KeyboardButton("World🌎")],
[KeyboardButton("Russia🇷🇺")]
]
reply_markup = ReplyKeyboardMarkup(keyboard,
one_time_keyboard=True,
resize_keyboard=True)
bot.send_message(update.message.chat_id, "Choose the category🔍",
reply_markup=reply_markup)
return ADD_CATEGORY
def add_category(update, context):
chat_data = context.chat_data
chat_data['category'] = update.message.text
context.bot.send_message(update.effective_chat.id,
text='Type the keyword✏️')
return ADD_KEYWORD
def enter_rss(update, context):
context.bot.send_message(update.effective_chat.id,
text='Enter RSS✏️')
return ADD_RSS
def add_rss(update, context):
chat_data = context.chat_data
chat_data['rss'] = update.message.text
context.bot.send_message(update.effective_chat.id,
text='Type the keyword✏️')
return RSS_KEYWORD
def news_search_rss(update, context):
chat_data = context.chat_data
keyword = update.message.text
rss = chat_data['rss']
article = rss_news(rss, keyword)
if article:
for i in article:
context.bot.send_message(chat_id=update.effective_chat.id,
text=f"{i}")
else:
context.bot.send_message(chat_id=update.effective_chat.id,
text="Sorry, I can't find appropriate news😔\nTry to enter another keyword🌸", )
def news_search(update, context):
global dictionary
chat_data = context.chat_data
keyword = update.message.text
category = dictionary[chat_data['category']]
article = news(category, keyword)
if article:
for i in article:
context.bot.send_message(chat_id=update.effective_chat.id,
text=f"{i}")
else:
context.bot.send_message(chat_id=update.effective_chat.id,
text="Sorry, I can't find appropriate news😔\nTry to enter another keyword🌸", )
def cancel(update, context):
return ConversationHandler.END
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)
choose_category_conversation = ConversationHandler(
entry_points=[MessageHandler(Filters.regex("category"),
choose_category),
MessageHandler(Filters.regex("rss"),
enter_rss)],
states={
ADD_CATEGORY: [MessageHandler(Filters.text,
add_category)],
ADD_KEYWORD: [MessageHandler(Filters.text,
news_search),
MessageHandler(Filters.regex("category"),
choose_category)],
ADD_RSS: [MessageHandler(Filters.text, add_rss)],
RSS_KEYWORD: [MessageHandler(Filters.text,
news_search_rss),
MessageHandler(Filters.regex("rss"),
enter_rss)]
},
fallbacks=[MessageHandler(Filters.all,cancel)])
dispatcher.add_handler(choose_category_conversation)
logging.info("start")
updater.start_polling(poll_interval=1)