forked from teddavis4/colette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolette.py
379 lines (324 loc) · 12.9 KB
/
colette.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env python
from uuid import uuid4
import re
from telegram import InlineQueryResultArticle, ParseMode, \
InputTextMessageContent
from telegram.ext import (Updater, InlineQueryHandler, CommandHandler, Filters,
MessageHandler)
import logging
import sys
import requests
import urllib.parse
#from bsearch import find_book
#import bemail
import sqlite3
import random
import subprocess
import string
from Markov import Markov
from pymarkovchain import MarkovChain
from telegram.ext.dispatcher import run_async
mc = MarkovChain("./markov")
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
# 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):
bot.sendMessage(update.message.chat_id, text='Hi!')
def help(bot, update):
bot.sendMessage(update.message.chat_id, text='Help!')
def search_quote_by_id(id):
"""Search for a quote
"""
with sqlite3.connect('quipper') as conn:
c = conn.cursor()
c.execute('select * from quotes where x=?', (id,))
quote = c.fetchone()[3]
return quote
def search_quote(search_str):
"""Search for a quote
"""
s_str = "%%%{0}%%%".format(search_str)
with sqlite3.connect('quipper') as conn:
c = conn.cursor()
c.execute('select * from quotes where quote like ? order by date desc'
' limit 1', (s_str,))
quote = c.fetchone()[3]
return quote
def get_random_quote(user=None):
"""Get random quote from global or from user
"""
with sqlite3.connect('quipper') as conn:
c = conn.cursor()
c.execute('select * from quotes')
return random.choice(c.fetchall())[3]
@run_async
def figlet(bot, update):
"""Return a figlet string
"""
fig_str = ' '.join(update.message.text.split()[1:])
if fig_str.startswith('-'):
bot.sendMessage(update.message.chat_id, text='No, dude')
else:
fig = subprocess.check_output(['/usr/bin/figlet',
'{}'.format(fig_str)]).decode()
fig_str = """.
```{}```""".format(fig)
bot.sendMessage(update.message.chat_id, text=fig_str,
parse_mode="Markdown")
def delete_quote_by_id(bot, update):
"""Delete a quote from telegram
"""
id = update.message.from_user.id
if id != 127511991:
return
quote_id = update.message.text.split()[1]
try:
with sqlite3.connect('quipper') as conn:
c = conn.cursor()
c.execute('delete from quotes where x=?', (quote_id, ))
conn.commit()
bot.sendMessage(update.message.chat_id, text="I deleted the quote "
" with ID {}".format(quote_id))
except Exception as e:
bot.sendMessage(update.message.chat_id, text="I couldn't delete the"
" quote. ({})".format(e))
def get_random_user_quote(user):
"""Get random quote from a user
"""
with sqlite3.connect('quipper') as conn:
c = conn.cursor()
c.execute('select * from users where username=?', (user,))
id = c.fetchone()[0]
c.execute('select * from quotes where owner=? order by date desc' , (id,
))
quote = random.choice(c.fetchall())
quote_id = quote[0]
quote_date = quote[1]
quote_text = quote[3]
return "[{0}]At {1} @{2} said ``{3}''".format(quote_id, quote_date, user,
quote_text)
def get_last_quote(user):
"""Get last quote from a user
"""
with sqlite3.connect('quipper') as conn:
c = conn.cursor()
c.execute('select * from users where username=?', (user,))
id = c.fetchone()[0]
c.execute('select * from quotes where owner=? order by date desc'
' limit 1', (id, ))
quote = c.fetchone()
quote_id = quote[0]
quote_date = quote[1]
quote_text = quote[3]
return "[{0}]At {1} @{2} said ``{3}''".format(quote_id, quote_date, user,
quote_text)
def get_quote(bot, update):
"""Get a quote based on the request
"""
line_s = update.message.text.split()
if len(line_s)<=1:
bot.sendMessage(update.message.chat_id, text=get_random_quote())
if line_s[1] == '-l':
user = line_s[2].strip('@').strip()
bot.sendMessage(update.message.chat_id, text=get_last_quote(user))
elif line_s[1] == '-s':
search_str = ' '.join(line_s[2:])
bot.sendMessage(update.message.chat_id, text=search_quote(search_str))
elif line_s[1] == '-i':
id = line_s[2]
bot.sendMessage(update.message.chat_id, text=search_quote_by_id(id))
else:
user = line_s[1].strip('@').strip()
bot.sendMessage(update.message.chat_id,
text=get_random_user_quote(user))
#def restart_git(bot, update):
# """Restart the bot by exiting, forcing the container to reboot
# """
# try:
# git = subprocess.check_output(['/usr/bin/git', 'pull', '/usr/src/app'])
# except exception as e:
# pass
# sys.exit()
@run_async
def markov(bot, update):
"""Return a string of text from markov
"""
text = update.message.text.split()
if len(text) > 1:
seed = ' '.join(text[1:])
markov_text = mc.generateStringWithSeed(seed)
else:
markov_text = mc.generateString()
#with open('markov') as markov_file:
# chain = Markov(markov_file)
# markov_text = chain.generate_markov_text()
bot.sendMessage(update.message.chat_id, text=markov_text)
def channel_logger(bot, update):
"""Quip store
"""
time_or_times = 'times'
global buzzwords
global words
text = update.message.text
text_date = update.message.date
username = update.message.from_user.username
channel = update.message.chat.title
output = '@{} has said '.format(username)
for word in words:
lc_text = text.lower()
if word in text.lower():
c = len(lc_text.split(word))-1
# Reply with the count of gays.
if word not in buzzwords.keys():
buzzwords[word] = {}
gaycount = buzzwords[word].setdefault(username, 0) + c
if gaycount == 1:
time_or_times = 'time'
buzzwords[word][username] += c
if 'time' in output:
output += "; '{}' {} {}".format(word, gaycount, time_or_times)
else:
output += "'{}' {} {}".format(word, gaycount, time_or_times)
if 'time' in output:
bot.sendMessage(update.message.chat_id, text="{}"
" this session".format(output))
with open('markov_db', 'a') as f:
f.write('{0}\n'.format(text))
mc.generateDatabase(text)
dbdump = mc.dumpdb()
with open('telegram_log', 'a') as f:
f.write('{0} ({1}) [{2}]: {3}\n'.format(text_date, channel, username,
text))
def quipper_forward(bot, update):
"""Quip store
"""
quip = update.message.text
owner = update.message.forward_from.id
quote_date = update.message.forward_date
username = update.message.forward_from.username
with sqlite3.connect('quipper') as conn:
c = conn.cursor()
c.execute('select * from users where id=?', (owner,))
if not c.fetchone():
c.execute('insert into users values (?, ?)', (owner, username))
print('insert into quotes (date, owner, quote) values ({}, {}, {})',
(quote_date, owner, quip))
c.execute('insert into quotes (date, owner, quote) values (?, ?, ?)',
(quote_date, owner, quip))
conn.commit()
def quipper(bot, update):
"""Quip store
"""
quip = update.message.reply_to_message.text
owner = update.message.reply_to_message.from_user.id
quote_date = update.message.reply_to_message.date
username = update.message.reply_to_message.from_user.username
with sqlite3.connect('quipper') as conn:
c = conn.cursor()
c.execute('select * from users where id=?', (owner,))
if not c.fetchone():
c.execute('insert into users values (?, ?)', (owner, username))
print('insert into quotes (date, owner, quote) values ({}, {}, {})',
(quote_date, owner, quip))
c.execute('insert into quotes (date, owner, quote) values (?, ?, ?)',
(quote_date, owner, quip))
conn.commit()
def escape_markdown(text):
"""Helper function to escape telegram markup symbols"""
escape_chars = '\*_`\['
return re.sub(r'([%s])' % escape_chars, r'\\\1', text)
def register(bot, update):
userID = update.message.from_user['id']
email = update.message.text.split()[1]
try:
with sqlite3.connect('books') as conn:
c = conn.cursor()
c.execute("insert into users values (?, ?)", (userID, email))
conn.commit()
bot.sendMessage(update.message.chat_id, text="You have been added to the"
" database with the email address '{0}'".format(email))
except Exception as e:
bot.sendMessage(update.message.chat_id, text="You are already in the"
" database")
def email(bot, update):
userID = update.message.from_user['id']
with sqlite3.connect('books') as conn:
c = conn.cursor()
c.execute("select * from users where telegram_name=?", (userID, ))
email_addr = c.fetchone()[1]
uuid = update.message.text.split()[1]
mail_output = bemail.find_book(uuid, True, str(email_addr))
if not mail_output or mail_output == "null":
bot.sendMessage(update.message.chat_id, text="I emailed your book to"
" you")
def search(bot, update):
query = update.message.text.split(' ', 1)[1]
if len(query)<=2:
bot.sendMessage(update.message.chat_id, text="You gotta ask for a book")
#return str(find_book(query))
book_results = find_book(query)
msg_reply = '\n'.join(book_results.split('\n')[:3])
bot.sendMessage(update.message.chat_id, text=str(msg_reply))
@run_async
def get_ifl_link(bot, update):
query = update.message.text.split(' ', 1)[1]
var = requests.get( r'http://www.google.com/search?q={0}&btnI'.format(
urllib.parse.quote_plus(query) ))
bot.sendMessage(update.message.chat_id, text=var.url)
def inlinequery(bot, update):
query = update.inline_query.query
results = list()
results.append(InlineQueryResultArticle(id=uuid4(), title="Caps",
input_message_content=InputTextMessageContent( query.upper())))
results.append(InlineQueryResultArticle(id=uuid4(), title="Bold",
input_message_content=InputTextMessageContent( "*%s*" %
escape_markdown(query), parse_mode=ParseMode.MARKDOWN)))
results.append(InlineQueryResultArticle(id=uuid4(), title="Italic",
input_message_content=InputTextMessageContent( "_%s_" %
escape_markdown(query), parse_mode=ParseMode.MARKDOWN)))
bot.answerInlineQuery(update.inline_query.id, results=results)
def error(bot, update, error):
logger.warn('Update "%s" caused error "%s"' % (update, error))
def main():
global buzzwords
global words
words = ['gay', 'something something', 'nigger', 'i mean', 'guttersnipe']
buzzwords = {}
# Create the Updater and pass it your bot's token.
updater = Updater("239641029:AAET8NqR9uef_JccleEY9oHsZsdvw4-ZD7Y")
# 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))
dp.add_handler(CommandHandler("register", register))
dp.add_handler(CommandHandler("bemail", email))
dp.add_handler(CommandHandler("bsearch", search))
dp.add_handler(CommandHandler("google", get_ifl_link))
dp.add_handler(CommandHandler("Google", get_ifl_link))
dp.add_handler(CommandHandler("quote", quipper))
dp.add_handler(CommandHandler("quip", quipper))
dp.add_handler(CommandHandler("getq", get_quote))
dp.add_handler(CommandHandler("getquote", get_quote))
dp.add_handler(CommandHandler("delquote", delete_quote_by_id))
dp.add_handler(CommandHandler("fig", figlet))
dp.add_handler(CommandHandler("markov", markov))
#dp.add_handler(CommandHandler("restart", restart_git))
# on noncommand i.e message - echo the message on Telegram
dp.add_handler(InlineQueryHandler(inlinequery))
# log all errors
dp.add_error_handler(error)
# Add message handler for quips!
#dp.add_handler(MessageHandler([Filters.text], quipper))
dp.add_handler(MessageHandler([Filters.text], channel_logger))
# 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()