-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.py
113 lines (104 loc) · 3.71 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
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
import json
import requests
import urllib
import os
class Bot():
def __init__(self):
self.heroku_env = False
if os.environ.get('SECRET_TOKEN'):#verifica se variável do heroku foi setada
self.heroku_env = True
self.TOKEN = os.environ['SECRET_TOKEN']
else:
self.TOKEN = self.get_infos_file('/token_my_routinebot.txt', False)
self.URL = 'https://api.telegram.org/bot{}/'.format(self.TOKEN)
self.HELP = (
'/new NOME\n'
'/todo ID\n'
'/doing ID\n'
'/done ID\n'
'/delete ID\n'
'/rename ID NOME\n'
'/dependson ID ID...\n'
'/duplicate ID\n'
'/priority ID PRIORITY{low, medium, high}\n'
'/duedate ID DATE{dd/mm/aaaa}\n'
'/list\n'
'/help\n'
)
def get_infos_file(self, input_file, set_password=False):
'''
Pega informações como token e identificações do arquivo de acordo
com os parametros input_file e set_password
'''
with open('my_routinebot_files' + input_file) as infile:
if set_password is True:
lines = infile.readline()[1:]
lines = infile.readline()
info = ""
for line in lines:
info += line
info = info.rstrip('\n')
return info
def make_github_issue(self, title, body=None):
'''
Cria nova issue no repositório de acordo com a url
'''
url = 'https://api.github.com/repos/TecProg-20181/my_routinebot/issues'
session = requests.Session()
session.auth = (self.get_infos_file("/username_git.txt", False),
self.get_infos_file("/username_git.txt", True))
if self.heroku_env:
session.auth = (os.environ['GIT_USER'],\
os.environ['GIT_PASSWORD'])
else:
session.auth = (self.get_infos_file("/username_git.txt", False),
self.get_infos_file("/username_git.txt", True))
issue = {'title': title,
'body': body}
r = session.post(url, json.dumps(issue))
if r.status_code == 201:
print('Successfully created Issue {0:s}'.format(title))
else:
print('Could not create Issue {0:s}'.format(title))
print('Response:', r.content)
def get_url(self, url):
'''
Retorna a url desejada
'''
response = requests.get(url)
content = response.content.decode("utf8")
return content
def get_json_from_url(self, url):
'''
Retorna json a partir da url desejada
'''
content = self.get_url(url)
js = json.loads(content)
return js
def get_updates(self, offset=None):
'''
Retorna updates do bot com timeout = 100
'''
url = self.URL + 'getUpdates?timeout=100'
if offset:
url += '&offset={}'.format(offset)
js = self.get_json_from_url(url)
return js
def send_message(self, text, chat_id, reply_markup=None):
'''
Retorna mensagem a ser mostrada para o usuário pelo bot
'''
text = urllib.parse.quote_plus(text)
url = self.URL + 'sendMessage?text={}&chat_id={}&parse_mode=Markdown'\
.format(text, chat_id)
if reply_markup:
url += '&reply_markup={}'.format(reply_markup)
self.get_url(url)
def get_last_update_id(self, updates):
'''
Pega id do último update do bot
'''
update_ids = []
for update in updates['result']:
update_ids.append(int(update['update_id']))
return max(update_ids)