This repository has been archived by the owner on Sep 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain.py
executable file
·219 lines (191 loc) · 8.98 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import telebot
import requests
import feedparser
import dota2api
import HTMLParser
from utils import intime, getCID, getContent, loadjson, addUser, deljson, bot, addBlogPostInstantView
from settings import DOTA2API_TOKEN
telebot.logger.setLevel(logging.ERROR)
api = dota2api.Initialise(DOTA2API_TOKEN)
heroes_list = api.get_heroes()
heroes_list = heroes_list["heroes"]
parser = HTMLParser.HTMLParser()
@bot.message_handler(commands=['start'])
def send_welcome(message):
bot.reply_to(message, "Howdy, how are you doing?")
@bot.message_handler(commands=['help'])
def get_help(message):
gitUrl = "https://github.com/zachkont/dotaUpdatesBot"
redditUrl = "https://www.reddit.com/message/compose/?to=karaflix"
bot.reply_to(message,
u'Did you find a bug or have any questions?\n'
+ u'Visit the [GitHub page]({gitUrl})'.format(gitUrl=gitUrl)
+ u' for more information or contact'
+ u' [/u/karaflix]({redditUrl})'.format(redditUrl=redditUrl)
+ u' on reddit!',
parse_mode="Markdown",
disable_web_page_preview=True)
@bot.message_handler(commands=['info'])
def get_info(message):
gitUrl = "https://github.com/zachkont/dotaUpdatesBot"
bot.reply_to(message,
u'The main use of this bot is to notify you whenever a Dota update gets released.\n'
+ u'Visit the [GitHub page]({gitUrl})'.format(gitUrl=gitUrl)
+ u' for more information on what you can do or how to contribute!',
parse_mode="Markdown",
disable_web_page_preview=True)
@bot.message_handler(commands=['dotanews', 'dotanew', 'dnews', 'dnew'])
def dota_news(message):
if intime(message):
cid = getCID(message)
content = getContent(message)
url = "http://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid=570&count=1&maxlength=300&format=json"
request = requests.get(url)
data = request.json()
if content != "?":
if request.status_code == 200:
title = data['appnews']['newsitems'][0]['title']
content = data['appnews']['newsitems'][0]['contents']
content_nice = content.replace(" - ", "\n - ")
content_nice = content_nice.replace("*", "\n*")
content_nice = parser.unescape(content_nice)
url = data['appnews']['newsitems'][0]['url']
bot.send_message(
cid,
u'*{title}*```> \n{content_nice}\n```'.format(title=title, content_nice=content_nice)
+ u'[More info here]({url})'.format(url=url),
parse_mode="Markdown",
disable_web_page_preview=True)
else:
bot.reply_to(
message,
"`There has been an error, the number {error} to be specific.`"
.format(error=request.status_code),
parse_mode="Markdown")
else:
bot.reply_to(
message,
"`Send this command alone and I will show you the last Steam News for Dota2 entry`",
parse_mode="Markdown")
@bot.message_handler(commands=['dotablog', 'dotablognew', 'dblog', 'dblognew'])
def dota_blog(message):
if intime(message):
cid = getCID(message)
dota_blog_rss_url = "http://blog.dota2.com/feed/"
feed = feedparser.parse(dota_blog_rss_url)
post_title = feed["items"][0]["title"]
post_link = addBlogPostInstantView(feed["items"][0]["link"])
bot.send_message(
cid,
u'*{title}* ```\n\n```'
.format(title = post_title)
+ u'[Read the latest blog post in your browser]({url})'
.format(url = post_link),
parse_mode = "Markdown")
@bot.message_handler(commands=['subscribe', 'letmeknow'])
def start_subscription(message):
uid = str(message.from_user.id)
cid = str(message.chat.id)
if message.chat.type == 'private':
if uid not in loadjson("userlist"):
addUser(uid, message.from_user.first_name, "userlist")
bot.send_message(uid, 'You will now receive updates!', parse_mode="Markdown")
else:
bot.send_message(uid, 'halloooooo', parse_mode="Markdown")
elif message.chat.type == 'group' or message.chat.type == 'supergroup':
if cid not in loadjson("grouplist"):
bot.reply_to(
message,
'Hello fans, this awesome group is now added and I will keep you guys up to date',
parse_mode="Markdown")
gid = str(message.chat.id)
gname = message.chat.title
addUser(gid, gname, "grouplist")
else:
bot.send_message(cid, 'Hello again fans, your group has already been registered', parse_mode="Markdown")
else:
pass
@bot.message_handler(commands=['unsubscribe', 'enough', 'unsub'])
def end_subscription(message):
uid = str(message.from_user.id)
cid = str(message.chat.id)
if message.chat.type == 'private':
if uid in loadjson("userlist"):
deljson(uid, "userlist")
bot.send_message(uid, 'You have been removed from the subscription list', parse_mode="Markdown")
else:
bot.send_message(uid, 'You can\'t unsubscribe without subscribing first dummy!', parse_mode="Markdown")
elif message.chat.type == 'group' or message.chat.type == 'supergroup':
if cid in loadjson("grouplist"):
bot.reply_to(message, 'Oh, no more updates? Okay...', parse_mode="Markdown")
gid = str(message.chat.id)
deljson(gid, "grouplist")
else:
bot.send_message(cid, 'Not subscibed yet', parse_mode="Markdown")
else:
pass
@bot.message_handler(regexp="match (\d.*?)(\D|$)|match")
def find_match(message):
if intime(message):
cid = getCID(message)
content = getContent(message)
match_id = message.text
if match_id != '/match':
match_id = match_id.split()[1]
try:
match = api.get_match_details(match_id)
url = match.url
request = requests.get(url)
match_data = request.json()
if content != "?":
if request.status_code == 200:
hero_list = []
if match_data['result']['radiant_win']:
title = "Radiant!"
else:
title = "Dire!"
url = "http://www.dotabuff.com/matches/" + match_id
radiant_content = ""
dire_content = ""
for player in match_data['result']['players']:
if player['player_slot'] < 100: # radiant
for hero in heroes_list:
if hero['id'] == player['hero_id']:
hero_list.append(hero['localized_name'])
radiant_content = (radiant_content +
hero['localized_name'] + " " +
str(player['kills']) + "/" +
str(player['deaths']) + "/" +
str(player['assists']) + '\n')
else: # dire
for hero in heroes_list:
if hero['id'] == player['hero_id']:
hero_list.append(hero['localized_name'])
dire_content = (dire_content +
hero['localized_name'] + " " +
str(player['kills']) + "/" +
str(player['deaths']) + "/" +
str(player['assists']) + '\n')
bot.send_message(
cid,
'Winner: *{title}* \n _Radiant:_ \n{radiant}\n _Dire:_\n{dire}\n'
.format(title=title, radiant=radiant_content, dire=dire_content)
+ '[Dotabuff link]({url})'.format(url=url),
parse_mode="Markdown",
disable_web_page_preview=True)
else:
bot.reply_to(
message,
"`There has been an error, the number {error} to be specific.`".format(error=request.status_code),
parse_mode="Markdown")
else:
bot.reply_to(message, "`wat`", parse_mode="Markdown")
except Exception as ex:
bot.reply_to(
message,
"There has been an error, its message is:\n `{error}`".format(error=ex.msg),
parse_mode="Markdown")
bot.polling(none_stop=True, interval=0)