Skip to content

Commit

Permalink
Merge pull request #46 from jlmadurga/bug/not_handle_when_no_text_mes…
Browse files Browse the repository at this point in the history
…sage

Only handle text messages
  • Loading branch information
jlmadurga committed Apr 14, 2016
2 parents 497cffa + 7f1ebb8 commit 106ad88
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
8 changes: 8 additions & 0 deletions microbot/views/hooks/telegram_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

logger = logging.getLogger(__name__)

class OnlyTextMessages(Exception):
pass


class TelegramHookView(APIView):

Expand All @@ -26,6 +29,8 @@ def create_update(self, serializer, bot):
except Chat.DoesNotExist:
chat, _ = Chat.objects.get_or_create(**serializer.data['message']['chat'])

if 'text' not in serializer.data['message']:
raise OnlyTextMessages
message, _ = Message.objects.get_or_create(message_id=serializer.data['message']['message_id'],
from_user=user,
date=datetime.fromtimestamp(serializer.data['message']['date']),
Expand All @@ -52,6 +57,9 @@ def post(self, request, hook_id):
handle_update.delay(update.id, bot.id)
else:
logger.error("Update %s ignored by disabled bot %s" % (update, bot))
except OnlyTextMessages:
logger.warning("Not text message %s for bot %s" % (request.data, hook_id))
return Response(status=status.HTTP_200_OK)
except:
exc_info = sys.exc_info()
traceback.print_exception(*exc_info)
Expand Down
9 changes: 8 additions & 1 deletion tests/functional/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from rest_framework.authtoken.models import Token
from django.apps import apps
import json
from rest_framework import status
try:
from unittest import mock
except ImportError:
Expand Down Expand Up @@ -79,7 +80,13 @@ def test_handler_request_no_cascade(self):
self.handler = factories.HandlerFactory(bot=self.bot)
self.assertEqual(Handler.objects.count(), 1)
Request.objects.all().delete()
self.assertEqual(Handler.objects.count(), 1)
self.assertEqual(Handler.objects.count(), 1)

def test_no_text_message(self):
update = json.loads(self.update.to_json())
update['message'].pop('text')
response = self.client.post(self.webhook_url, json.dumps(update), **self.kwargs)
self.assertEqual(status.HTTP_200_OK, response.status_code)

class TestRequests(LiveServerTestCase, testcases.BaseTestBot):

Expand Down

0 comments on commit 106ad88

Please sign in to comment.