telegram: update to Telegram Bot api v.13.0
This commit is contained in:
parent
a69962724a
commit
8fda5d4d82
@ -7,9 +7,10 @@
|
||||
#
|
||||
# GPL v3 license
|
||||
|
||||
from telegram import (ParseMode)
|
||||
from telegram import (ParseMode, Update)
|
||||
|
||||
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, RegexHandler,
|
||||
ConversationHandler, Job)
|
||||
ConversationHandler, Job, CallbackContext)
|
||||
|
||||
import logging
|
||||
import subprocess
|
||||
@ -25,7 +26,7 @@ timers = dict()
|
||||
social_log_last_ids = dict()
|
||||
|
||||
|
||||
def start(bot, update):
|
||||
def start(update: Update, context: CallbackContext):
|
||||
|
||||
user = update.message.from_user
|
||||
logger.info("User %s started the conversation." % user.first_name)
|
||||
@ -33,15 +34,25 @@ def start(bot, update):
|
||||
update.message.reply_text(
|
||||
'Hi! This is the openmamba GNU/Linux Bot.\n'
|
||||
'Send any text to search for packages.\n'
|
||||
'Send /details name to see details of specified source package.\n'
|
||||
'Send /details [name] to see details of specified source package.\n'
|
||||
'Send /set [seconds] to enable notifications.\n'
|
||||
'Send /unset to disable notifications.\n'
|
||||
'Send /cancel to stop talking to me.\n\n')
|
||||
'Send /unset to disable notifications.\n\n')
|
||||
|
||||
return
|
||||
|
||||
|
||||
def query(bot, update):
|
||||
def help(update: Update, context: CallbackContext):
|
||||
|
||||
update.message.reply_text(
|
||||
'Send any text to search for packages.\n'
|
||||
'Send /details [name] to see details of specified source package.\n'
|
||||
'Send /set [seconds] to enable notifications.\n'
|
||||
'Send /unset to disable notifications.\n\n')
|
||||
|
||||
return
|
||||
|
||||
|
||||
def query(update: Update, context: CallbackContext):
|
||||
user = update.message.from_user
|
||||
logger.info("Query of %s: %s" % (user.first_name, update.message.text))
|
||||
|
||||
@ -63,7 +74,7 @@ def query(bot, update):
|
||||
return
|
||||
|
||||
|
||||
def details(bot, update, args):
|
||||
def details(update: Update, context: CallbackContext):
|
||||
user = update.message.from_user
|
||||
logger.info("Details of %s: %s" % (user.first_name, update.message.text))
|
||||
|
||||
@ -72,7 +83,7 @@ def details(bot, update, args):
|
||||
conn = sqlite3.connect('/var/webbuild/db/%s-sources.db' % rep, check_same_thread=False)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
'SELECT id,name,version,release,summary,url,description FROM sources where name="%s"' % (args[0]))
|
||||
'SELECT id,name,version,release,summary,url,description FROM sources where name="%s"' % (context.args[0]))
|
||||
for row in cursor:
|
||||
response += "<b>%s</b> %s-%s (devel)\n%s\n%s\n\n<i>%s</i>\n\n" % (row[1], row[2], row[3], row[4], row[5], row[6])
|
||||
for arch in [ 'x86_64', 'i586', 'arm' ]:
|
||||
@ -84,16 +95,16 @@ def details(bot, update, args):
|
||||
response += "\n\n\n"
|
||||
|
||||
if response != "":
|
||||
update.message.reply_text(response, parse_mode=ParseMode.HTML)
|
||||
update.message.reply_text(response[0:4096], parse_mode=ParseMode.HTML)
|
||||
else:
|
||||
update.message.reply_text('No results found.')
|
||||
return
|
||||
|
||||
|
||||
def alarm(bot, job):
|
||||
def alarm(context):
|
||||
social_log_conn = sqlite3.connect('/var/webbuild/webbuild.db')
|
||||
social_log_cursor = social_log_conn.cursor()
|
||||
social_log_cursor.execute('SELECT id,user,text,datetime(time,\'localtime\'),type FROM social_log where id>%s' % social_log_last_ids[job.context])
|
||||
social_log_cursor.execute('SELECT id,user,text,datetime(time,\'localtime\'),type FROM social_log where id>%s' % social_log_last_ids[context.job])
|
||||
response = ""
|
||||
for row in social_log_cursor:
|
||||
if row[4] == "job":
|
||||
@ -101,20 +112,21 @@ def alarm(bot, job):
|
||||
else:
|
||||
response += "<i>%s</i> %s (%s)\n" % (row[1], row[2], row[3])
|
||||
if response != "":
|
||||
bot.sendMessage(job.context, response, parse_mode=ParseMode.HTML)
|
||||
social_log_last_ids[job.context] = row[0]
|
||||
bot.sendMessage(context.job, response, parse_mode=ParseMode.HTML)
|
||||
social_log_last_ids[context.job] = row[0]
|
||||
|
||||
|
||||
def set(bot, update, args, job_queue):
|
||||
def set(update: Update, context: CallbackContext):
|
||||
chat_id = update.message.chat_id
|
||||
try:
|
||||
due = int(args[0])
|
||||
due = int(context.args[0])
|
||||
if due < 0:
|
||||
update.message.reply_text('Sorry we can not go back to future!')
|
||||
return
|
||||
job = Job(alarm, due, repeat=True, context=chat_id)
|
||||
timers[chat_id] = job
|
||||
job_queue.put(job)
|
||||
#job = Job(alarm, context, next_t=due, repeat=True, context=chat_id)
|
||||
#timers[chat_id] = job
|
||||
context.job_queue.run_repeating(alarm, due)
|
||||
#context.jobqueue.put(job)
|
||||
|
||||
social_log_conn = sqlite3.connect('/var/webbuild/webbuild.db')
|
||||
social_log_cursor = social_log_conn.cursor()
|
||||
@ -127,7 +139,7 @@ def set(bot, update, args, job_queue):
|
||||
update.message.reply_text('Usage: /set <seconds>')
|
||||
|
||||
|
||||
def unset(bot, update):
|
||||
def unset(update: Update, context: CallbackContext):
|
||||
chat_id = update.message.chat_id
|
||||
|
||||
if chat_id not in timers:
|
||||
@ -140,7 +152,7 @@ def unset(bot, update):
|
||||
update.message.reply_text('Notifications disabled')
|
||||
|
||||
|
||||
def cancel(bot, update):
|
||||
def cancel(update: Update, context: CallbackContext):
|
||||
user = update.message.from_user
|
||||
logger.info("User %s canceled the conversation." % user.first_name)
|
||||
update.message.reply_text('Bye!')
|
||||
@ -148,8 +160,8 @@ def cancel(bot, update):
|
||||
return ConversationHandler.END
|
||||
|
||||
|
||||
def error(bot, update, error):
|
||||
logger.warn('Update "%s" caused error "%s"' % (update, error))
|
||||
def error(update: Update, context: CallbackContext):
|
||||
logger.warning('Update "%s" caused error "%s"' % (update, context.error))
|
||||
|
||||
|
||||
def main():
|
||||
@ -161,7 +173,7 @@ def main():
|
||||
name, var = line.rstrip().split("=")
|
||||
if name == "TELEGRAM_BOT_TOKEN":
|
||||
bot_token = var
|
||||
|
||||
|
||||
# Create the EventHandler and pass it your bot's token.
|
||||
updater = Updater(bot_token)
|
||||
|
||||
@ -169,11 +181,11 @@ def main():
|
||||
dp = updater.dispatcher
|
||||
|
||||
dp.add_handler(CommandHandler('start', start))
|
||||
dp.add_handler(MessageHandler(Filters.text, query))
|
||||
dp.add_handler(CommandHandler('help', help))
|
||||
dp.add_handler(CommandHandler('details', details, pass_args=True))
|
||||
dp.add_handler(CommandHandler('set', set, pass_args=True, pass_job_queue=True))
|
||||
dp.add_handler(CommandHandler('unset', unset))
|
||||
dp.add_handler(CommandHandler('cancel', cancel))
|
||||
dp.add_handler(MessageHandler(Filters.text, query))
|
||||
|
||||
# log all errors
|
||||
dp.add_error_handler(error)
|
||||
|
Loading…
Reference in New Issue
Block a user