Added openmamba bot for telegram

This commit is contained in:
Silvan Calarco 2017-12-30 23:14:48 +01:00
parent 38c75eea33
commit 058a0baa7e
3 changed files with 205 additions and 1 deletions

View File

@ -11,7 +11,7 @@ sysconfdir = /etc
sbindir = ${exec_prefix}/sbin sbindir = ${exec_prefix}/sbin
bindir = ${exec_prefix}/bin bindir = ${exec_prefix}/bin
datadir = ${prefix}/share datadir = ${prefix}/share
initrddir = ${sysconfdir}/rc.d/init.d unitdir = /lib/systemd/system
libexecdir = ${prefix}/libexec libexecdir = ${prefix}/libexec
mandir = ${prefix}/usr/share/man mandir = ${prefix}/usr/share/man
sysconfigdir= ${sysconfdir}/sysconfig sysconfigdir= ${sysconfdir}/sysconfig
@ -52,6 +52,7 @@ install-dirs:
@$(INSTALL_DIR) $(DESTDIR)$(sbindir) @$(INSTALL_DIR) $(DESTDIR)$(sbindir)
@$(INSTALL_DIR) $(DESTDIR)$(cgidir) @$(INSTALL_DIR) $(DESTDIR)$(cgidir)
@$(INSTALL_DIR) $(DESTDIR)$(htmldir) @$(INSTALL_DIR) $(DESTDIR)$(htmldir)
@$(INSTALL_DIR) $(DESTDIR)$(unitdir)
@$(INSTALL_DIR) $(DESTDIR)$(pck_datadir) @$(INSTALL_DIR) $(DESTDIR)$(pck_datadir)
@$(INSTALL_DIR) $(DESTDIR)$(pck_statedir) @$(INSTALL_DIR) $(DESTDIR)$(pck_statedir)
@$(INSTALL_DIR) $(DESTDIR)$(pck_statedir)/template/autoupdate/spec-patches-build/old @$(INSTALL_DIR) $(DESTDIR)$(pck_statedir)/template/autoupdate/spec-patches-build/old
@ -82,6 +83,7 @@ install-programs:
@$(INSTALL_SCRIPT) webbuild/webbuild-checkpassword $(DESTDIR)$(libexecdir)/webbuild-checkpassword @$(INSTALL_SCRIPT) webbuild/webbuild-checkpassword $(DESTDIR)$(libexecdir)/webbuild-checkpassword
@$(INSTALL_SCRIPT) webbuild/webbuild-functions $(DESTDIR)$(pck_datadir)/webbuild-functions @$(INSTALL_SCRIPT) webbuild/webbuild-functions $(DESTDIR)$(pck_datadir)/webbuild-functions
@$(INSTALL_SCRIPT) webbuild/webbuild-functions-private $(DESTDIR)$(pck_datadir)/webbuild-functions-private @$(INSTALL_SCRIPT) webbuild/webbuild-functions-private $(DESTDIR)$(pck_datadir)/webbuild-functions-private
@$(INSTALL_SCRIPT) telegram/openmamba_bot.py $(DESTDIR)$(pck_datadir)/openmamba_bot.py
@cp -a webbuild/html/* $(DESTDIR)$(htmldir) @cp -a webbuild/html/* $(DESTDIR)$(htmldir)
install-data: install-data:
@ -102,6 +104,7 @@ install-data:
@$(INSTALL_DATA) autodist-bashrc $(DESTDIR)$(pck_statedir)/.bashrc @$(INSTALL_DATA) autodist-bashrc $(DESTDIR)$(pck_statedir)/.bashrc
@$(INSTALL_DATA) autodist-bash_profile $(DESTDIR)$(pck_statedir)/.bash_profile @$(INSTALL_DATA) autodist-bash_profile $(DESTDIR)$(pck_statedir)/.bash_profile
@$(INSTALL_DATA) autodist-tmpfilesd $(DESTDIR)$(prefix)/lib/tmpfiles.d/autodist.conf @$(INSTALL_DATA) autodist-tmpfilesd $(DESTDIR)$(prefix)/lib/tmpfiles.d/autodist.conf
@$(INSTALL_DATA) telegram/openmambabot.service $(DESTDIR)$(unitdir)/openmambabot.service
@touch $(DESTDIR)$(pck_statedir)/template/autoupdate/auto.success @touch $(DESTDIR)$(pck_statedir)/template/autoupdate/auto.success
@touch $(DESTDIR)$(pck_statedir)/template/autoupdate/auto.skip @touch $(DESTDIR)$(pck_statedir)/template/autoupdate/auto.skip

191
telegram/openmamba_bot.py Executable file
View File

@ -0,0 +1,191 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# openmamba bot for Telegram
#
# Copyright (C) 2016-2017 by Silvan Calarco <silvan.calarco@mambasoft.it>
#
# GPL v3 license
from telegram import (ParseMode)
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, RegexHandler,
ConversationHandler, Job)
import logging
import subprocess
import sqlite3
import os
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
timers = dict()
social_log_last_ids = dict()
def start(bot, update):
user = update.message.from_user
logger.info("User %s started the conversation." % user.first_name)
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 /set [seconds] to enable notifications.\n'
'Send /unset to disable notifications.\n'
'Send /cancel to stop talking to me.\n\n')
return
def query(bot, update):
user = update.message.from_user
logger.info("Query of %s: %s" % (user.first_name, update.message.text))
response = ""
for rep in [ 'devel', 'devel-games', 'devel-makedist', 'devel-autodist', 'devel-kernel', 'devel-misc', 'devel-kde4' ]:
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 FROM sources where name like "%%%s%%" or summary like "%%%s%%" '
'ORDER BY name="%s" DESC, name like "%s%%" DESC, name like "%%%s%%" DESC limit 10'
% (update.message.text, update.message.text, update.message.text, update.message.text, update.message.text))
for row in cursor:
response += "<b>%s</b> %s-%s (%s)\n%s\n%s\n\n" % (row[1], row[2], row[3], rep, row[4], row[5])
if response != "":
update.message.reply_text(response, parse_mode=ParseMode.HTML)
else:
update.message.reply_text('No results found.')
return
def details(bot, update, args):
user = update.message.from_user
logger.info("Details of %s: %s" % (user.first_name, update.message.text))
response = ""
for rep in [ 'devel', 'devel-games', 'devel-makedist', 'devel-autodist', 'devel-kernel', 'devel-misc', 'devel-kde4' ]:
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]))
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' ]:
conn1 = sqlite3.connect('/var/webbuild/db/' + rep + '-' + arch + '.db', check_same_thread=False)
cursor1 = conn1.cursor()
cursor1.execute('SELECT id,name FROM packages where id_source=%s' % (row[0]))
for row1 in cursor1:
response += "<b>%s</b>(%s) " % (row1[1], arch)
response += "\n\n\n"
if response != "":
update.message.reply_text(response, parse_mode=ParseMode.HTML)
else:
update.message.reply_text('No results found.')
return
def alarm(bot, job):
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])
response = ""
for row in social_log_cursor:
if row[4] == "job":
response += "Job run by <i>%s</i> %s (%s)\n" % (row[1], row[2], row[3])
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]
def set(bot, update, args, job_queue):
chat_id = update.message.chat_id
try:
due = int(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)
social_log_conn = sqlite3.connect('/var/webbuild/webbuild.db')
social_log_cursor = social_log_conn.cursor()
social_log_cursor.execute('''SELECT MAX(id) FROM social_log''')
social_log_last_ids[chat_id] = social_log_cursor.fetchone()[0] - 5
update.message.reply_text('Notifications enabled!')
except (IndexError, ValueError):
update.message.reply_text('Usage: /set <seconds>')
def unset(bot, update):
chat_id = update.message.chat_id
if chat_id not in timers:
update.message.reply_text('Notification were not enabled')
return
job = timers[chat_id]
job.schedule_removal()
del timers[chat_id]
update.message.reply_text('Notifications disabled')
def cancel(bot, update):
user = update.message.from_user
logger.info("User %s canceled the conversation." % user.first_name)
update.message.reply_text('Bye!')
return ConversationHandler.END
def error(bot, update, error):
logger.warn('Update "%s" caused error "%s"' % (update, error))
def main():
# Load bot token from external configuration file
bot_token = ""
with open("/etc/autodist/secrets") as myfile:
for line in myfile:
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)
# Get the dispatcher to register handlers
dp = updater.dispatcher
dp.add_handler(CommandHandler('start', start))
dp.add_handler(MessageHandler(Filters.text, query))
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))
# log all errors
dp.add_error_handler(error)
# Start the Bot
updater.start_polling()
# Run the bot until the you 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()

View File

@ -0,0 +1,10 @@
[Unit]
Description=openmamba bot for Telegram
After=syslog.target
[Service]
Type=simple
ExecStart=/usr/bin/python /usr/share/autodist/openmamba_bot.py
[Install]
WantedBy=multi-user.target