-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwtfvartha.py
113 lines (86 loc) · 3.21 KB
/
wtfvartha.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
from pymongo import MongoClient # The PyMongo distribution contains tools for interacting with MongoDB database from Python.
from pprint import pprint # The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter.
import datetime
import pymongo
import time
import requests
import json
import os
from dotenv import load_dotenv # Python-dotenv reads key-value pairs from a .env file and can set them as environment variables.
load_dotenv()
TOKEN = os.getenv('TOKEN')
MONGOURL = os.getenv('MONGOURL')
client = pymongo.MongoClient(MONGOURL)
URL = "https://api.telegram.org/bot{}/".format(TOKEN)
def get_url(url):
response = requests.get(url)
content = response.content.decode("utf8")
return content
def get_json_from_url(url):
content = get_url(url)
js = json.loads(content)
return js
def get_update():
url = URL + "getUpdates"
js = get_json_from_url(url)
return js
def get_chat_id_and_text(updates):
now = datetime.datetime.now()
chat_list = []
for i in updates['result']:
if i['message']['chat']['id'] not in chat_list:
chat_list.append(i['message']['chat']['id'])
print("######################################################################")
print("""\
_ _ _ ___ ___ _ _ _ _
| | | |_ _| __> | | |___ _ _ _| |_ | |_ ___
| | | || || _> | ' <_> | '_> | | | . <_> |
|__/_/ |_||_| |__/<___|_| |_| |_|_<___| by Athul
""")
print("######################################################################")
print()
print("Loading chat ids.......")
print()
for i in chat_list:
print("chat id -> ", i)
print()
news_text = get_news_text()
return (news_text, chat_list)
def get_news_text():
now = datetime.datetime.now()
url = ('https://newsapi.org/v2/top-headlines?''country=in&''apiKey=YOUR API KEY')
print()
print("Getting news json form -> \n\b", url)
print()
print("######################################################################")
news = requests.get(url).json()
article = news['articles']
news_list = [u'\u2705 WTFVartha Headlines \n',
now.strftime("%Y-%m-%d"), now.strftime("%H:%M")]
for i in article:
news_list.append(i['title']+"\n")
text = u"\n\u23E9 "
text = text.join(news_list)
news_text = text
return (news_text)
def send_message(text, chat_id):
url = URL + "sendMessage?text={}&chat_id={}".format(text, chat_id)
js = get_json_from_url(url)
if js['ok'] == True:
print("message sent to", chat_id)
print()
else:
print("messege failed to sent : ", js['description'])
print()
def main():
while True:
text, chat_list = get_chat_id_and_text(get_update())
for chat_i in chat_list:
print("Trying to sent message to chat id ->", chat_i)
send_message(text, chat_i)
send_message("\u2705", chat_i)
break
if __name__ == '__main__':
main()
# https://api.telegram.org/bot<YourBOTToken>/getUpdates
# https://www.rapidtables.com/code/text/unicode-characters.html (emoji link)