-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bot.py
58 lines (41 loc) · 1.82 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
import Constants as keys
import Response as R
from telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, CallbackContext, Filters
import json
import sys
sys.path.append('News')
import News
def start_command(update: Update, context: CallbackContext) -> None:
update.message.reply_text(f'Hello {update.effective_user.first_name}')
def help_command(update: Update, context: CallbackContext) -> None:
update.message.reply_text("How can I help you? \n 1. Read news -> /news <number of news>")
def news_command(update: Update, context: CallbackContext):
try:
limit_news = int(context.args[0])
news = News.GetNews(limit_news)
for x in range(0, len(news)):
message = json.loads(news[x])
update.message.reply_text(message['title'] + "\n"
+ message['link'] + "\n" + message['description'])
except (IndexError, ValueError):
update.message.reply_text('Please choose the number of news displayed!')
def handle_message(update: Update, context: CallbackContext):
text = str(update.message.text).lower()
response = R.sample_response(text)
update.message.reply_text(response)
def error(update: Update, context: CallbackContext):
print(f"Update {update} cause error {context.error}")
def main():
updater = Updater(keys.API_KEY)
dp = updater.dispatcher
dp.add_handler(CommandHandler('start', start_command))
dp.add_handler(CommandHandler('help', help_command))
dp.add_handler(CommandHandler("news", news_command))
dp.add_handler(MessageHandler(Filters.text, handle_message))
updater.start_polling()
updater.idle()
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
main()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/