Skip to content

Instantly share code, notes, and snippets.

@glader
Last active December 13, 2016 20:53
Show Gist options
  • Save glader/de676c620bde984b39387f7424348f9e to your computer and use it in GitHub Desktop.
Save glader/de676c620bde984b39387f7424348f9e to your computer and use it in GitHub Desktop.
telegram bot
# coding: utf8
# https://github.com/python-telegram-bot/python-telegram-bot
from telegram.ext import Updater, MessageHandler, CommandHandler, Filters, Job
import datetime
import logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
log = logging.getLogger(__name__)
timers = dict()
def alarm(bot, job):
"""Function to send the alarm message"""
bot.sendMessage(job.context, text='Сделал что-нибудь?')
def start(bot, update, job_queue):
"""Adds a job to the queue"""
chat_id = update.message.chat_id
log.info('Start timer for %s', chat_id)
# Add job to queue
job = Job(alarm, 60*60, repeat=True, context=chat_id)
timers[chat_id] = job
job_queue.put(job)
update.message.reply_text('За работу!')
def stop(bot, update):
"""Removes the job if the user changed their mind"""
chat_id = update.message.chat_id
log.info('Stop timer for %s', chat_id)
if chat_id not in timers:
update.message.reply_text('You have no active timer')
return
job = timers[chat_id]
job.schedule_removal()
del timers[chat_id]
update.message.reply_text('Наработался чтоли?')
def add(bot, update):
log.info('add %s', update.message.text)
message = '%s %s\n' % (datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'), update.message.text.encode('utf8'))
open('data/%s.txt' % update.message.from_user.id, 'a').write(message)
update.message.reply_text('Записал!')
def last(bot, update):
lines = open('data/%s.txt' % update.message.from_user.id).readlines()[-10:]
if lines:
get_date = lambda line: line[:10]
last_date = get_date(lines[0])
result = []
for line in lines:
date, time, message = line.split(' ', 2)
d = datetime.datetime.strptime(date + ' ' + time, '%Y-%m-%d %H:%M:%S') + datetime.timedelta(hours=5)
if get_date(line) != last_date:
result.append('\n%s\n' % d.strftime('%d %b'))
decorated = '%s %s' % (d.strftime('%H:%M'), message.capitalize())
result.append(decorated)
last_date = get_date(line)
update.message.reply_text(''.join(result))
else:
update.message.reply_text('Ты еще ничего не сделал')
def help(bot, update):
update.message.reply_text(open('help').read().decode('utf8'))
def error_callback(bot, update, error):
log.error(error)
updater = Updater(open('token').read())
log.info('Run')
updater.dispatcher.add_handler(CommandHandler('start', start, pass_job_queue=True))
updater.dispatcher.add_handler(CommandHandler('stop', stop))
updater.dispatcher.add_handler(CommandHandler('last', last))
updater.dispatcher.add_handler(CommandHandler('help', help))
updater.dispatcher.add_handler(MessageHandler(Filters.text, add))
updater.dispatcher.add_error_handler(error_callback)
updater.start_polling()
updater.idle()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment