-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
82 lines (61 loc) · 2.31 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
78
79
80
81
82
import os
from dotenv import load_dotenv
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from apidata import get_headlines,search_headline,get_articles_as_text
load_dotenv()
# Getting News Data
headlinesNum = ""
helptext = """
Hi there 👋,
I am tbot and I provide news to you
1. Type /news **category** to recieve top 5 headlines of that category (change every hour)
categories : (business entertainment general health science sports technology)
for example : /news technology
2. Type /search **search query** to search for a headline
"""
def start(update, context):
update.message.reply_text(helptext)
def help(update, context):
update.message.reply_text(helptext)
def search_for_news(update, context):
search_q = update.message.text
if "/search" in search_q:
search_q = search_q[8:]
print(search_q)
newsarr = search_headline(search_q)
reply_news = get_articles_as_text(newsarr)
update.message.reply_text(f'{reply_news}')
else :
update.message.reply_text('I cannot understand the input try /help ')
def news(update, context):
category_q = update.message.text
if "/news" in category_q:
category_q = category_q[6:]
print(category_q)
try:
headlineArr = get_headlines(category_q,"5")
reply_news = get_articles_as_text(headlineArr)
update.message.reply_text(f'{reply_news}')
except :
print(error)
update.message.reply_text('📡 Nothing Check your internet 📡')
else :
update.message.reply_text('I cannot understand the input try /help ')
def error(update, context):
update.message.reply_text('Ummm I think something is not fine')
def text(update, context):
update.message.reply_text('I cannot understand')
def main():
TOKEN = os.getenv('BOT_TOKEN')
updater = Updater(TOKEN, use_context=True)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("help", help))
dispatcher.add_handler(CommandHandler("search", search_for_news))
dispatcher.add_handler(CommandHandler("news", news))
dispatcher.add_handler(MessageHandler(Filters.text, text))
dispatcher.add_error_handler(error)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()