Skip to content

Instantly share code, notes, and snippets.

@solyard
Created April 27, 2017 15:36
Show Gist options
  • Save solyard/43d6e889a98964ce9a3bdfedd5bbeba3 to your computer and use it in GitHub Desktop.
Save solyard/43d6e889a98964ce9a3bdfedd5bbeba3 to your computer and use it in GitHub Desktop.
def forward_messages(bot, update):
upd = update.to_dict()
"""MASS SAVING"""
chatid = upd['message']['chat']['id']
msg = upd['message']['text']
if msg == '/cleardb':
pass
elif msg == '/done':
currentdate = str(datetime.datetime.now().date()).replace('-', '.')
out_text = ''
owner = upd['message']['from']['first_name']
cursor.execute('SELECT * FROM forward_msg WHERE id = 0')
keys = ['id', 'owner', 'frm', 'msg']
db_list = OrderedDict(zip(keys, *cursor.fetchall()))
username = db_list['frm'].split('\t')
usermessage = db_list['msg'].split('\t')
for n, t in zip(username, usermessage):
if n == '' or t == '':
pass
else:
out_text += '*[*{0}*]*\n{1}\n\n'.format(n,t)
print(out_text)
if out_text == '':
bot.sendMessage(chat_id=chatid, text='Нет пересланных сообщений для сохранения!')
else:
cursor.execute("INSERT INTO quotes (date, quote, owner, rate) VALUES ('%s', '%s', '%s', '%d')"
% (currentdate, out_text, owner, 0))
cursor.execute("UPDATE forward_msg SET owner ='', frm ='', msg ='' WHERE id = 0")
cursor.execute('SELECT max(id) FROM quotes')
bot.sendMessage(chat_id=chatid, text='Цитата сохранена под №' + str(cursor.fetchall()[0][0]))
con.commit()
try:
frwrd = upd['message']['forward_from']['first_name']
if frwrd:
frm = str(frwrd + '\t')
msgs = str(upd['message']['text'] + '\t')
cursor.execute("UPDATE forward_msg SET frm =CONCAT(frm,'%s'), msg =CONCAT(msg,'%s') WHERE id = 0" % (str(frm), str(msgs)))
con.commit()
except KeyError:
print('False!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment