From 28b08fdef48175904e2da79842393a598ec8aae7 Mon Sep 17 00:00:00 2001 From: Eugeniy Kuznetsov Date: Sat, 28 Apr 2018 20:49:20 +0700 Subject: [PATCH 01/12] change start message --- src/inftybot/core/intents/start.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/inftybot/core/intents/start.py b/src/inftybot/core/intents/start.py index 4abcaf6..ad526d1 100644 --- a/src/inftybot/core/intents/start.py +++ b/src/inftybot/core/intents/start.py @@ -13,4 +13,4 @@ def get_handler(cls): return CommandHandler("start", cls.as_callback(), pass_chat_data=True, pass_user_data=True) def handle(self, *args, **kwargs): - self.update.message.reply_text(_("Let's start")) + self.update.message.reply_text(_("Let's start. Use /login to begin.")) From c6a6997ebdf4324c8bc76bf5718cd33a222453b0 Mon Sep 17 00:00:00 2001 From: Eugeniy Kuznetsov Date: Sat, 28 Apr 2018 20:50:45 +0700 Subject: [PATCH 02/12] fix messages --- src/inftybot/authentication/intents/base.py | 3 ++- src/inftybot/core/intents/base.py | 5 ++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/inftybot/authentication/intents/base.py b/src/inftybot/authentication/intents/base.py index dcc6c5f..ed8dc5e 100644 --- a/src/inftybot/authentication/intents/base.py +++ b/src/inftybot/authentication/intents/base.py @@ -51,4 +51,5 @@ def before_validate(self): self.set_api_authentication(self.current_user) if not self.is_authenticated: - raise AuthenticationError("Please, /login first") + message = "Please, talk to @{} to login".format(self.bot.username) + raise AuthenticationError(message) diff --git a/src/inftybot/core/intents/base.py b/src/inftybot/core/intents/base.py index c193c47..78499f8 100644 --- a/src/inftybot/core/intents/base.py +++ b/src/inftybot/core/intents/base.py @@ -128,7 +128,6 @@ def handle(self, *args, **kwargs): def handle_error(self, error): logger.error('Unhandled error in the intent {}: {}'.format(self.__class__, error)) - pass @classmethod def create_from_intent(cls, intent, **kwargs): @@ -159,7 +158,7 @@ def __init__(self, **kwargs): self.query = query def handle_error(self, error): - raise NotImplementedError + self.bot.send_message(chat_id=self.update.effective_chat.id, text=error.message) def parse_query(self): """ @@ -193,7 +192,7 @@ def get_handler(cls): ) def handle_error(self, error): - self.update.message.reply_text(error.message) + self.bot.send_message(chat_id=self.update.effective_chat.id, text=error.message) class BaseCommandIntent(BaseIntent): From 9b46b4cba5b70027aadcc82b47d353a1657665fb Mon Sep 17 00:00:00 2001 From: Eugeniy Kuznetsov Date: Sat, 28 Apr 2018 22:06:48 +0700 Subject: [PATCH 03/12] select language from DEFAULT_LANGUAGE (defaults to 'en') on topic_render --- README.md | 10 ++++++++++ src/inftybot/config.py | 3 +++ src/inftybot/topics/templates/topics/topic.md | 6 ++++-- src/inftybot/topics/templatetags/__init__.py | 1 + .../topics/templatetags/langsplit_tags.py | 14 ++++++++++++++ src/inftybot/topics/tests/test_render_topic.py | 17 +++++++++++++++++ 6 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/inftybot/topics/templatetags/__init__.py create mode 100644 src/inftybot/topics/templatetags/langsplit_tags.py create mode 100644 src/inftybot/topics/tests/test_render_topic.py diff --git a/README.md b/README.md index 458a10e..7f7d1f7 100644 --- a/README.md +++ b/README.md @@ -201,3 +201,13 @@ Please, fill `` of the Amazon SNS topic ``` Note the ```telegram/webhook``` url part + + + +(check last video sent) + ++ . Make the replies about log-in go directly to user from bot, rather than to channel. ++ . Slightly refactor my PR with notifcations to general channels about Goal, Idea, Plan,... +. Add links to inf.li, rather than to API items. +. Have a default language of the bot, and remove language tokens from text when showing on TG +. Prepend the language token when language of bot is known (e.g., like on web client.) less diff --git a/src/inftybot/config.py b/src/inftybot/config.py index d4fec9d..e26e86d 100644 --- a/src/inftybot/config.py +++ b/src/inftybot/config.py @@ -14,6 +14,9 @@ SEARCH_PREVIEW_LENGTH = os.environ.get('SEARCH_PREVIEW_LENGTH', 200) INLINE_QUERY_CACHE_TIME = os.environ.get('INLINE_QUERY_CACHE_TIME', 0) +DEFAULT_LANGUAGE = os.environ.get('DEFAULT_LANGUAGE', 'en') + + INTENTS = [ 'inftybot.core.intents.start.StartCommandIntent', 'inftybot.core.intents.reset.ResetCommandIntent', diff --git a/src/inftybot/topics/templates/topics/topic.md b/src/inftybot/topics/templates/topics/topic.md index ae59792..c960c5a 100644 --- a/src/inftybot/topics/templates/topics/topic.md +++ b/src/inftybot/topics/templates/topics/topic.md @@ -1,10 +1,12 @@ +{% load langsplit_tags %} + {% if object.categories_names %} `Categories:` {{object.categories_names|join:','}} {% endif %} -`{{object.get_type_display}}:` *{{object.title}}* +`{{object.get_type_display}}:` *{{object.title|select_language}}* -{{object.body}} +{{object.body|select_language}} *URL:* {{object.url}} _Reply to this message to post a comment on Infinity._ \ No newline at end of file diff --git a/src/inftybot/topics/templatetags/__init__.py b/src/inftybot/topics/templatetags/__init__.py new file mode 100644 index 0000000..57d631c --- /dev/null +++ b/src/inftybot/topics/templatetags/__init__.py @@ -0,0 +1 @@ +# coding: utf-8 diff --git a/src/inftybot/topics/templatetags/langsplit_tags.py b/src/inftybot/topics/templatetags/langsplit_tags.py new file mode 100644 index 0000000..cf82780 --- /dev/null +++ b/src/inftybot/topics/templatetags/langsplit_tags.py @@ -0,0 +1,14 @@ +# coding: utf-8 +from django import template +from langsplit import splitter + +from inftybot import config + +register = template.Library() + + +@register.filter +def select_language(value, langcode=None): + langcode = langcode or config.DEFAULT_LANGUAGE + splitted = splitter.split(value) + return splitted.get(langcode, value) diff --git a/src/inftybot/topics/tests/test_render_topic.py b/src/inftybot/topics/tests/test_render_topic.py new file mode 100644 index 0000000..e4d9cb3 --- /dev/null +++ b/src/inftybot/topics/tests/test_render_topic.py @@ -0,0 +1,17 @@ +# coding: utf-8 +from django.test import TestCase + +from inftybot.topics.models import Topic +from inftybot.topics.utils import render_topic + + +class RenderLangsplitTestCase(TestCase): + def test_default_language_renders_ok(self): + title = ".:en:Title" + body = """.:en + body + """ + + instance = Topic(title=title, body=body) + rendered = render_topic(instance) + self.assertNotIn('.:en', rendered) From 386ac1fe4065fcdf2279692bd204462aceb37393 Mon Sep 17 00:00:00 2001 From: Eugeniy Kuznetsov Date: Sat, 28 Apr 2018 22:18:38 +0700 Subject: [PATCH 04/12] get_topic_url using client --- README.md | 2 +- src/inftybot/comments/intents/comment.py | 7 ++---- src/inftybot/config.py | 3 +++ src/inftybot/topics/models.py | 9 ++++++++ src/inftybot/topics/templates/topics/topic.md | 2 +- src/inftybot/topics/utils.py | 22 +++++++++++++++++++ 6 files changed, 38 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7f7d1f7..7db3cf8 100644 --- a/README.md +++ b/README.md @@ -209,5 +209,5 @@ Note the ```telegram/webhook``` url part + . Make the replies about log-in go directly to user from bot, rather than to channel. + . Slightly refactor my PR with notifcations to general channels about Goal, Idea, Plan,... . Add links to inf.li, rather than to API items. -. Have a default language of the bot, and remove language tokens from text when showing on TG ++ . Have a default language of the bot, and remove language tokens from text when showing on TG . Prepend the language token when language of bot is known (e.g., like on web client.) less diff --git a/src/inftybot/comments/intents/comment.py b/src/inftybot/comments/intents/comment.py index a07bdeb..a534535 100644 --- a/src/inftybot/comments/intents/comment.py +++ b/src/inftybot/comments/intents/comment.py @@ -10,6 +10,7 @@ from inftybot.authentication.intents.base import AuthenticatedMixin from inftybot.core.intents.base import BaseMessageIntent +from inftybot.topics.utils import get_topic_id _ = gettext logger = logging.getLogger(__name__) @@ -69,11 +70,7 @@ def get_topic_url(self): def get_topic_id(self): topic_url = self.get_topic_url() - - try: - return int(topic_url.strip('/').rsplit('/', 1)[-1]) - except (IndexError, TypeError): - return None + return get_topic_id(topic_url) class TopicReplyFilter(BaseFilter): diff --git a/src/inftybot/config.py b/src/inftybot/config.py index e26e86d..6495b17 100644 --- a/src/inftybot/config.py +++ b/src/inftybot/config.py @@ -40,3 +40,6 @@ JINJA_EXTENSIONS = [ 'inftybot.contrib.jinja2.ext.StringExtension', ] + +TOPIC_URL_TEMPLATE = 'https://inf.li/#/{INFTY_API_URL}:en/@/topic/{TOPIC_ID}' +COMMENT_URL_TEMPLATE = 'https://inf.li/#/{INFTY_API_URL}:en/@/topic/{TOPIC_ID}#comment-{COMMENT_ID}' diff --git a/src/inftybot/topics/models.py b/src/inftybot/topics/models.py index 525e473..209b04d 100644 --- a/src/inftybot/topics/models.py +++ b/src/inftybot/topics/models.py @@ -1,8 +1,11 @@ # coding: utf-8 + from django.contrib.postgres.fields import ArrayField from django.db import models from django.utils.translation import gettext as _ +from inftybot.topics.utils import get_topic_id, get_topic_client_url + class Topic(models.Model): """Dummy topic model""" @@ -53,3 +56,9 @@ def categories_str(self): def categories_str(self, value): # NOQA pass + + def get_topic_id(self): + return get_topic_id(self.url) + + def get_client_url(self): + return get_topic_client_url(self) diff --git a/src/inftybot/topics/templates/topics/topic.md b/src/inftybot/topics/templates/topics/topic.md index c960c5a..e8f3401 100644 --- a/src/inftybot/topics/templates/topics/topic.md +++ b/src/inftybot/topics/templates/topics/topic.md @@ -8,5 +8,5 @@ {{object.body|select_language}} -*URL:* {{object.url}} +*URL:* {{object.get_client_url}} _Reply to this message to post a comment on Infinity._ \ No newline at end of file diff --git a/src/inftybot/topics/utils.py b/src/inftybot/topics/utils.py index 623dbab..8d566cc 100644 --- a/src/inftybot/topics/utils.py +++ b/src/inftybot/topics/utils.py @@ -1,6 +1,28 @@ # coding: utf-8 +from urllib.parse import urlparse + from django.template.loader import render_to_string +from inftybot import config + + +def get_topic_id(topic_url): + try: + return int(topic_url.strip('/').rsplit('/', 1)[-1]) + except (IndexError, TypeError): + return None + + +def get_topic_client_url(instance): + template = config.TOPIC_URL_TEMPLATE + url_object = urlparse(config.INFTY_API_URL) + topic_id = instance.get_topic_id() + context = { + 'INFTY_API_URL': url_object.netloc, + 'TOPIC_ID': topic_id, + } + return template.format(**context) + def render_topic(instance): return render_to_string('topics/topic.md', context={'object': instance}) From 5594e61809c43b17c1d66c5ece6c40ebd6a282a1 Mon Sep 17 00:00:00 2001 From: Eugeniy Kuznetsov Date: Sat, 28 Apr 2018 22:40:23 +0700 Subject: [PATCH 05/12] remove comment url --- src/inftybot/comments/intents/comment.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/inftybot/comments/intents/comment.py b/src/inftybot/comments/intents/comment.py index a534535..d2d9956 100644 --- a/src/inftybot/comments/intents/comment.py +++ b/src/inftybot/comments/intents/comment.py @@ -106,11 +106,11 @@ def handle(self, *args, **kwargs): } try: - response = self.api.client.comments.post(data=data) + self.api.client.comments.post(data=data) except (HttpClientError, HttpServerError) as e: logger.error(e) else: self.bot.sendMessage( chat_id=self.update.message.chat_id, - text="Comment was created: {}".format(response.get('url', 'URL UNDEFINED')), + text="Comment was created", ) From fdf440445b3f70552943acda30e8b752e14dcc1e Mon Sep 17 00:00:00 2001 From: Eugeniy Kuznetsov Date: Sat, 28 Apr 2018 22:40:33 +0700 Subject: [PATCH 06/12] remove comment url template --- src/inftybot/config.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/inftybot/config.py b/src/inftybot/config.py index 6495b17..88dd284 100644 --- a/src/inftybot/config.py +++ b/src/inftybot/config.py @@ -42,4 +42,3 @@ ] TOPIC_URL_TEMPLATE = 'https://inf.li/#/{INFTY_API_URL}:en/@/topic/{TOPIC_ID}' -COMMENT_URL_TEMPLATE = 'https://inf.li/#/{INFTY_API_URL}:en/@/topic/{TOPIC_ID}#comment-{COMMENT_ID}' From ecd313c487b627b0ecd8760182dd210727c28262 Mon Sep 17 00:00:00 2001 From: Eugeniy Kuznetsov Date: Sat, 28 Apr 2018 23:01:57 +0700 Subject: [PATCH 07/12] prepare comment with langcode --- src/inftybot/comments/intents/comment.py | 4 +++- src/inftybot/comments/utils.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 src/inftybot/comments/utils.py diff --git a/src/inftybot/comments/intents/comment.py b/src/inftybot/comments/intents/comment.py index d2d9956..0017a3e 100644 --- a/src/inftybot/comments/intents/comment.py +++ b/src/inftybot/comments/intents/comment.py @@ -9,6 +9,7 @@ from telegram.ext import MessageHandler, BaseFilter from inftybot.authentication.intents.base import AuthenticatedMixin +from inftybot.comments.utils import prepare_comment from inftybot.core.intents.base import BaseMessageIntent from inftybot.topics.utils import get_topic_id @@ -99,9 +100,10 @@ def handle(self, *args, **kwargs): original_message = self.update.message.reply_to_message parser = MessageParser(original_message.text) topic_url = parser.get_topic_url() + text = prepare_comment(self.update.message.text) data = { 'topic': topic_url, - 'text': self.update.message.text, + 'text': text.text, # 'languages': [], } diff --git a/src/inftybot/comments/utils.py b/src/inftybot/comments/utils.py new file mode 100644 index 0000000..f74bf2e --- /dev/null +++ b/src/inftybot/comments/utils.py @@ -0,0 +1,10 @@ +# coding: utf-8 +from inftybot import config + + +def prepare_comment(comment): + comment = comment.strip() + if comment.startswith('.:'): + # language provided, bypass + return comment + return ".:{langcode}\n{comment}".format(langcode=config.DEFAULT_LANGUAGE, comment=comment) From 87133d4d2ee9eba8791c8bf8c25b23c25ec2b174 Mon Sep 17 00:00:00 2001 From: Eugeniy Kuznetsov Date: Fri, 4 May 2018 00:55:34 +0700 Subject: [PATCH 08/12] fix select_language --- src/inftybot/topics/templatetags/langsplit_tags.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/inftybot/topics/templatetags/langsplit_tags.py b/src/inftybot/topics/templatetags/langsplit_tags.py index cf82780..17786ac 100644 --- a/src/inftybot/topics/templatetags/langsplit_tags.py +++ b/src/inftybot/topics/templatetags/langsplit_tags.py @@ -9,6 +9,10 @@ @register.filter def select_language(value, langcode=None): + if isinstance(value, list): + return [select_language(v) for v in value] langcode = langcode or config.DEFAULT_LANGUAGE splitted = splitter.split(value) - return splitted.get(langcode, value) + if isinstance(splitted, dict): + return splitted.get(langcode, value) + return value From c7135d10e21267262622c0ede02178dbd6cf69f0 Mon Sep 17 00:00:00 2001 From: Eugeniy Kuznetsov Date: Fri, 4 May 2018 00:56:05 +0700 Subject: [PATCH 09/12] assign categories on search --- src/inftybot/search/intents/search.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/inftybot/search/intents/search.py b/src/inftybot/search/intents/search.py index bccc4f9..ecb873c 100644 --- a/src/inftybot/search/intents/search.py +++ b/src/inftybot/search/intents/search.py @@ -48,11 +48,8 @@ def handle(self): def process_result(result): description = result.get('body', '')[:config.SEARCH_PREVIEW_LENGTH] - topic = Topic() - topic.type = result.get('type') - topic.title = result.get('title') - topic.body = result.get('body') - topic.url = result.get('url') + attrs = {k: v for k, v in result.items() if hasattr(Topic, k)} + topic = Topic(**attrs) message_text = render_topic(topic) From 8e0cf3710404ac328d0246cd25965a7efce9179f Mon Sep 17 00:00:00 2001 From: Eugeniy Kuznetsov Date: Fri, 4 May 2018 01:06:31 +0700 Subject: [PATCH 10/12] update topic template --- src/inftybot/topics/templates/topics/topic.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/inftybot/topics/templates/topics/topic.md b/src/inftybot/topics/templates/topics/topic.md index e8f3401..10b2258 100644 --- a/src/inftybot/topics/templates/topics/topic.md +++ b/src/inftybot/topics/templates/topics/topic.md @@ -1,8 +1,6 @@ {% load langsplit_tags %} -{% if object.categories_names %} -`Categories:` {{object.categories_names|join:','}} -{% endif %} +{% if object.categories_names %}`Categories:` {{object.categories_names|select_language|join:', '}}{% endif %} `{{object.get_type_display}}:` *{{object.title|select_language}}* From 2e7ae3038b2b32ad3fa4422cca235eeae4c59481 Mon Sep 17 00:00:00 2001 From: Eugeniy Kuznetsov Date: Fri, 4 May 2018 01:11:56 +0700 Subject: [PATCH 11/12] fix get_topic_id --- src/inftybot/topics/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/inftybot/topics/utils.py b/src/inftybot/topics/utils.py index 8d566cc..a7ca213 100644 --- a/src/inftybot/topics/utils.py +++ b/src/inftybot/topics/utils.py @@ -9,7 +9,7 @@ def get_topic_id(topic_url): try: return int(topic_url.strip('/').rsplit('/', 1)[-1]) - except (IndexError, TypeError): + except (AttributeError, IndexError, TypeError): return None From 8f4c7d7fc33b7680172d5efa08d555fd58600758 Mon Sep 17 00:00:00 2001 From: Eugeniy Kuznetsov Date: Fri, 4 May 2018 01:14:57 +0700 Subject: [PATCH 12/12] fix tests --- src/inftybot/authentication/tests/test_login.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/inftybot/authentication/tests/test_login.py b/src/inftybot/authentication/tests/test_login.py index b0f80a1..7cfaa9c 100644 --- a/src/inftybot/authentication/tests/test_login.py +++ b/src/inftybot/authentication/tests/test_login.py @@ -39,7 +39,9 @@ def test_call_intent_with_user_ensure_api_request_contains_token(self): except ValidationError: pass - self.assertEqual(intent.api.session.headers['authorization'], 'Token token') + store = getattr(intent.api.client, '_store') + session = store['session'] + self.assertEqual(session.headers['authorization'], 'Token token') def test_before_validate_sets_api_authentication(self): # todo test