This repository has been archived by the owner on Apr 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
281 lines (218 loc) · 8.85 KB
/
app.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import telepot
import traceback
import time
from datetime import datetime
from urllib.request import urlopen
import urllib3
import os
import firebase_admin
from firebase_admin import credentials, db
from github import Github
import tweepy
import json
import re
bot = telepot.Bot(os.environ['BOT_TOKEN'])
bot.setWebhook("")
cred = credentials.Certificate(json.loads(os.environ['FIREBASE_KEY']))
app = firebase_admin.initialize_app(cred, {
'databaseURL': os.environ['FIREBASE_DB_URL']
})
gh = Github(os.environ['GITHUB_TOKEN'])
tw_auth = json.loads(os.environ['TWITTER_AUTH'])
oah = tweepy.OAuthHandler(tw_auth['consumer_key'], tw_auth['consumer_secret'])
oah.set_access_token(tw_auth['access_token'], tw_auth['access_token_secret'])
twitter = tweepy.API(oah)
def file_issue(repo_name, title, body):
global gh
repo = gh.get_repo("bspst/{}".format(repo_name))
if len(title.strip()) == 0:
return False
issue_title = title.strip()
issue_body = body.strip()
# File issue
issue = repo.create_issue(issue_title, body=issue_body)
return issue.number, "https://github.com/bspst/{}/issues/{}".format(repo_name, issue.number)
def parse_message(msg, access):
global app, gh, twitter, bot
sender = msg['from']
sender_id, sender_name = sender['id'], sender['first_name']
parts = msg['text'].strip().split()
cmd = parts[0][1:]
body = msg['text'][len(parts[0]):]
print("Cmd: '{}'".format(cmd))
# Check if command is tagged
if '@' in cmd:
tagged_cmd = cmd.split('@')
cmd = tagged_cmd[0]
tag = tagged_cmd[1]
if tag != 'shiina_mashibot':
# Do nothing if not for bot
return
print("=>", cmd)
if cmd == "start":
return "I'm alive!"
if cmd == "ping":
return "Pong!"
if cmd == "hey":
return "Hey " + sender_name
if cmd == "echo":
if len(body.strip()) == 0:
return "You can't do nothing"
else:
return body
if cmd == "me":
if len(body.strip()) == 0:
return "You can't do nothing"
else:
return sender_name + body
if cmd == "fapped":
# Fap counter for https://github.com/bspst/todo/issues/21
ref = db.reference("/faps", app)
user_data = ref.child(str(sender_id))
current = user_data.get()
if current == None:
current = []
user_data.child(str(len(current))).set(int(round(time.time())))
return "DB updated! Total: {} faps.".format(len(current)+1)
if cmd == "fap":
arg = body.strip()
ref = db.reference("/faps", app)
user_data = ref.child(str(sender_id)).get()
if user_data == None:
user_data = []
if arg == "dump":
# Dump fap data
return "`{}`".format(json.dumps(user_data))
elif arg == "status":
# Fap statistics
if len(user_data) == 0:
return "Oh my sweet summer child..."
last_timestamp = list(user_data)[-1]
offset = 7 * 3600 # UTC+7 offset
last_fap = datetime.fromtimestamp(last_timestamp + offset).strftime('%Y-%m-%d %H:%M:%S')
fap_ago_str = format_time(time.time() - last_timestamp)
return "Total: {} faps, last: {} UTC+7 ({} ago)".format(len(user_data), last_fap, fap_ago_str)
else:
return "usage: /fap dump|status"
print("Access: {}".format(access))
# The commands below require a higher access level
if not access:
return "Sorry, you can't do that here."
if cmd == "todo":
# Make issue in bspst/todo
repo = gh.get_repo("bspst/todo")
if len(body.strip()) == 0:
return "You can't do nothing"
issue_title = body.split("\n")[0].strip()
issue_body = body[len(issue_title)+1:].strip()
issue_num, issue_url = file_issue("todo", "[{}] {}".format(sender['username'], issue_title), issue_body)
if issue_url:
return "Issue filed! [#{}]({})".format(issue_num, issue_url)
return "Unable to file issue"
if cmd == "issue":
# /issue file title\nbody
# /issue close repo number
args = body.split()
action = args[0]
if action == "file":
issue_repo = args[1]
issue_title = body.split("\n")[0][len(action)+len(issue_repo)+2:].strip()
issue_body = body[len(action)+len(issue_repo)+len(issue_title)+3:].strip()
issue_num, issue_url = file_issue(issue_repo, issue_title, "Opened by {}\n\n{}".format(sender['username'], issue_body))
if issue_url:
return "Issue [#{}]({}) filed on [bspst/{}]({})".format(issue_num, issue_url, issue_repo, "https://github.com/bspst/{}".format(issue_repo))
return "Unable to file issue"
elif action == "close":
repo_name = args[1]
issue_number = args[2]
# TODO
if cmd == "retweet":
# Retweet a tweet by its ID to @realbspst
if len(body.strip()) == 0:
if not 'reply_to_message' in msg:
return "You can't retweet nothing"
msg2tweet = msg['reply_to_message']
print("Reply msg:", list(msg2tweet.keys()))
if 'text' in msg2tweet:
twid = re.sub('(?:https:\/\/)twitter\.com\/([^?\/#]+)\/status\/([0-9]+).*', r'\2', msg2tweet['text'])
status = twitter.retweet(twid)
else:
twid = re.sub('(?: https:\/\/)twitter\.com\/([^?\/#]+)\/status\/([0-9]+).*', r'\2', body)
status = twitter.retweet(twid)
return "[Retweeted!](https://twitter.com/realbspst/status/{})".format(status.id)
if cmd == "tweet":
# Sends a tweet to @realbspst
if len(body.strip()) == 0:
if not 'reply_to_message' in msg:
return "You can't tweet nothing"
msg2tweet = msg['reply_to_message']
print("Reply msg:", list(msg2tweet.keys()))
if 'text' in msg2tweet:
body = "{}: {}".format(msg2tweet['from']['username'], msg2tweet['text'])
status = twitter.update_status(status=body)
elif 'photo' in msg2tweet:
photo_id = msg2tweet['photo'][-1]['file_id']
photo_path = bot.getFile(photo_id)['file_path'].split("/")[-1]
bot.download_file(photo_id, photo_path)
# photo_file = open("https://api.telegram.org/file/bot{}/{}".format(os.environ['BOT_TOKEN'], photo_path))
caption = "{}: {}".format(msg2tweet['from']['username'], msg2tweet['caption']) if 'caption' in msg2tweet else msg2tweet['from']['username']
status = twitter.update_with_media(photo_path, status=caption)
else:
status = twitter.update_status(status="{}: {}".format(sender['username'], body))
return "[Tweet posted!](https://twitter.com/realbspst/status/{})".format(status.id)
if cmd == "untweet":
# Deletes a tweet
if len(body.strip()) == 0:
if not 'reply_to_message' in msg:
# Delete last tweet
id2delete = twitter.user_timeline(count=1)[0].id
else:
# TODO
return
else:
if '/' in body:
id2delete = int(body.strip().split('/')[-1])
else:
id2delete = int(body.strip())
# Delete tweet
twitter.destroy_status(id2delete)
return "Untweeted message"
def format_time(seconds):
"""
Convert seconds to readable time format
"""
secs = int(seconds % 60)
mins = int(seconds // 60 % 60)
hours = int(seconds // 3600 % 24)
days = int(seconds // 86400)
return "{}d {}h {}m {}s".format(days, hours, mins, secs)
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
sender = msg['from']
sender_id, sender_name = sender['id'], sender['first_name']
print(content_type, chat_type, chat_id, sender_name)
print('< ', msg['text'])
print(list(msg.keys()))
# Limit access
access = True
if chat_id != int(os.environ['GROUP_ID']) and chat_type == "supergroup":
bot.sendMessage(chat_id, "Sorry, this is a private bot.")
bot.sendMessage(chat_id, "https://github.com/bspst/shiibot")
return
if chat_type != "supergroup":
access = False
reply = None
try:
if content_type == 'text':
if msg['text'].strip()[0] == '/':
reply = parse_message(msg, access)
except Exception as e:
reply = "```python\n{}```".format(traceback.format_exc())
if reply != None:
bot.sendMessage(chat_id, reply, parse_mode="Markdown", reply_to_message_id=msg['message_id'])
print('> ', reply)
bot.message_loop(handle)
print('Listening ...')
# Keep the program running.
while 1:
time.sleep(10)