-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnewsbot.py
153 lines (127 loc) · 5.31 KB
/
newsbot.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from telegram.ext import Updater, MessageHandler, CommandHandler, Filters
from watson_developer_cloud import ConversationV1
import json
import requests
from gtts import gTTS
import os
context = None
# 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):
print('Received /start command')
update.message.reply_text('Hi!')
def help(bot, update):
print('Received /help command')
update.message.reply_text('Help!')
def message(bot, update):
print('Received an update')
global context
conversation = ConversationV1(username='161a18e1-8ddc-404c-894b-26470e295d90', # TODO
password='d07Jb1MQduZM', # TODO
version='2018-02-16')
# get response from watson
response = conversation.message(
workspace_id='0cc6da39-2d3a-4848-807b-bac40520c026', # TODO
input={'text': update.message.text},
context=context)
print(json.dumps(response, indent=2))
context = response['context']
# build response
resp = ''
categ=''
ret=''
esp=''
res=''
es=''
ent=''
source=''
rett=''
temp=''
temp_min=''
temp_max=''
for text in response['intents']:
es += text['intent']
for text in response['entities']:
ent += text['entity']
if es!='Bot_Control_Approve_Response' and es!='final' and es!='':
for text in response['output']['text']:
resp += text
update.message.reply_text(resp)
print(8)
if es=='' and ent=='':
res = response['input']['text']
print(2)
n = requests.get('https://newsapi.org/v2/everything?q='+ res +'&sortBy=popularity&apiKey=85c3ead119524a58899bde57a9e38032')
obj = n.json()
re = int(obj['totalResults'])
ret = ''
ret += str(obj['articles'][4]['url'])+'\n'+'\n'
update.message.reply_text(ret)
elif es!='final' and es=='Bot_Control_Approve_Response':
# update.message.reply_text("http://127.0.0.1:5000/")
for text in response['output']['text']:
resp += text
count = response['context']['count']
category = response['context']['category']
n = requests.get(
'https://newsapi.org/v2/top-headlines?country=' + count + '&category=' + category + '&apiKey=0b2bb070c6074bcfa178b18b35a69ba9')
obj = n.json()
# ret+=str(obj['articles'][1]['url'])
print(7)
for i in range(0, 4):
ret += str(i + 1) + ': ' + str(obj['articles'][i]['title']) + '\n' + '\n'
update.message.reply_text(resp + '\n' + '\n' + ret)
elif es == '':
we = response['input']['text']
n = requests.get(
'http://api.openweathermap.org/data/2.5/weather?q=' + we + ',in&appid=ac7c75b9937a495021393024d0a90c44&units=metric')
obj = n.json()
ret = ''
ret += str(obj['weather'][0]['description'])
temp += str(obj['main']['temp'])
temp_min += str(obj['main']['temp_min'])
temp_max += str(obj['main']['temp_max'])
print(ret)
update.message.reply_text(
'Description :' + ret + '\nTemperature :' + temp + '\nMinimum Temperature :' + temp_min + '\nMaximum Temperature :' + temp_max)
else:
for text in response['output']['text']:
resp += text
num = ''
"""for text in response['output']:
num += text['del3']"""
num=response['output']['del3']
print(num)
count = response['output']['del']
print(count)
category = response['output']['del1']
n = requests.get(
'https://newsapi.org/v2/top-headlines?country=' + count + '&category=' + category + '&apiKey=0b2bb070c6074bcfa178b18b35a69ba9')
obj = n.json()
# source='we have got this news from '+str(obj['articles'][1323]['source']['name'])+'\n'+'\n'
ret += str(obj['articles'][num - 1]['url'])
update.message.reply_text(resp +'\n' +ret)
rett += str(obj['articles'][num - 1]['title'])
tts = gTTS(text=rett, lang='en')
tts.save("good.mp3")
os.system("cvlc good.mp3")
#update.message.reply_audio(audio='/home/sandesh/projects/project/newsbot/good.mp3')
#bot.send_audio(chat_id=systbotbot, audio=open('/home/sandesh/projects/project/newsbot/good.mp3', 'rb'))
def main():
# Create the Updater and pass it your bot's token.
updater = Updater('639931768:AAE4yJYO_HlMG7QL4RPwa0c4tScm3A-bQo4') # TODO
# 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(MessageHandler(Filters.text, message))
# 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()