diff options
author | Camil Staps | 2016-03-20 21:47:03 +0100 |
---|---|---|
committer | Camil Staps | 2016-03-20 21:47:03 +0100 |
commit | dd29c2ac5132fd926aeda6325d3fb21dfe09c49e (patch) | |
tree | 6367d356b8baa2e46524cb28969803c7a5534919 |
First version
-rw-r--r-- | .gitignore | 62 | ||||
-rw-r--r-- | LICENSE | 21 | ||||
-rw-r--r-- | README.md | 13 | ||||
-rw-r--r-- | kjvbot.py | 73 |
4 files changed, 169 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1dbc687 --- /dev/null +++ b/.gitignore @@ -0,0 +1,62 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +#Ipython Notebook +.ipynb_checkpoints @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Camil Staps + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..fd8fe68 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# KJVBot +Telegram bot for the King James Version + +To use this, you do not need to install the repository. Simply use [@KJV_bot](https://telegram.me/KJV_bot) on Telegram. This bot handles inline queries only. So, you can start typing in any chat: + +> @KJV_bot Jn 1:1 + +KJVBot will give a list of options of which you can choose one to send in the chat. + +If you are going to install KJVBot yourself, you should have the `bible` package installed. + +Copyright © 2016 Camil Staps. Licensed under MIT, see the LICENSE file. + diff --git a/kjvbot.py b/kjvbot.py new file mode 100644 index 0000000..83e2af8 --- /dev/null +++ b/kjvbot.py @@ -0,0 +1,73 @@ +import asyncio +import json +import urllib +from subprocess import check_output +import sys +import time + +import telepot +from telepot.async.delegate import per_inline_from_id, create_open + +def get_verses(ref): + verses = check_output(['bible', '-f', ref]).decode('utf-8').splitlines() + return map(lambda s:s.split(' ', 1), verses) + +def search(q): + q = q.split() + try: + ctx = list(filter(lambda s: s[0] == ':', q))[0][1:] + except IndexError: + ctx = '' + q = list(filter(lambda s: not s[0] == ':', q)) + print(ctx, q) + refs = check_output(['bible', '-f'] + q).decode('utf-8').splitlines()[2:22] + return map(get_verses, list(filter(lambda r: r.find(ctx) == 0, refs))) + +class InlineHandler(telepot.async.helper.UserHandler): + def __init__(self, seed_tuple, timeout): + super(InlineHandler, self).__init__(seed_tuple, timeout, + flavors=['inline_query', 'chosen_inline_result']) + + @asyncio.coroutine + def on_inline_query(self, msg): + query_id, _, query_string = telepot.glance(msg, flavor='inline_query') + + query_string = query_string.strip() + + articles = [] + if not query_string == '': + if not query_string[0] == '?': + for [ref, txt] in get_verses(query_string): + articles.append({'type': 'article', + 'id': ref, + 'title': ref, + 'message_text': '%s: %s' % (ref, txt), + 'description': txt }) + else: + for [[ref, txt]] in search(query_string): + articles.append({'type': 'article', + 'id': ref, + 'title': ref, + 'message_text': '%s: %s' % (ref, txt), + 'description': txt }) + + yield from self.bot.answerInlineQuery(query_id, articles[:20], 300) + + def on_chosen_inline_result(self, msg): + pass + + def on_close(*args, **kwargs): + pass + +TOKEN = sys.argv[1] + +bot = telepot.async.DelegatorBot(TOKEN, [ + (per_inline_from_id(), create_open(InlineHandler, timeout=10)), +]) +loop = asyncio.get_event_loop() + +loop.create_task(bot.messageLoop()) +print('Listening...') + +loop.run_forever() + |