def message():
    return Message(
        id=1,
        platform='telegram',
        text='',
        user=user,
        chat=chat,
        timestamp='',
        raw='',
    )
Exemple #2
0
    def build_message(self, data):
        '''
        Return a Message instance according to the data received from
        Facebook Messenger API.
        '''
        if not data:
            return None

        return Message(
            id=data['message']['mid'],
            platform=self.platform,
            text=data['message']['text'],
            user=data['sender']['id'],
            timestamp=data['timestamp'],
            raw=data,
            chat=None,  # TODO: Refactor build_messages and Message class
        )
Exemple #3
0
def test_render(mocked_settings):
    '''
    Test if a message template is rendered with a
    specified context.
    '''
    message = Message(123, 'Telegram', 'Bottery', 'text', 1506191745, 'raw')
    with tempfile.TemporaryDirectory() as tempdir:
        path = os.path.join(tempdir, 'templates')
        os.mkdir(path)

        template_path = os.path.join(path, 'template.md')
        with open(template_path, 'w') as template:
            template.write('{{ user }} {{ text }}!')

        mocked_settings.TEMPLATES = [path]
        rendered = render(message, 'template.md', {'text': 'rocks'})
        assert rendered == 'Bottery rocks!'
Exemple #4
0
    def build_message(self, data):
        '''
        Return a Message instance according to the data received from
        Telegram API.
        https://core.telegram.org/bots/api#update
        '''
        message_data = data.get('message')

        if message_data:
            return Message(
                id=message_data['message_id'],
                platform=self.platform,
                text=message_data['text'],
                user=TelegramUser(message_data['from']),
                timestamp=message_data['date'],
                raw=data,
            )
        else:
            return None
Exemple #5
0
    def build_message(self, data):
        '''
        Return a Message instance according to the data received from
        Telegram API.
        https://core.telegram.org/bots/api#update
        '''
        message_data = data.get('message') or data.get('edited_message')

        if not message_data:
            return None

        edited = 'edited_message' in data
        return Message(
            id=message_data['message_id'],
            platform=self.platform,
            text=message_data.get('text', ''),
            user=TelegramUser(message_data['from']),
            chat=TelegramChat(message_data['chat']),
            timestamp=message_data['date'],
            raw=data,
            edited=edited,
        )
Exemple #6
0
import datetime
import os
import tempfile
from unittest import mock

from bottery.message import Message, render

message = Message(123, 'Telegram', 'Bottery', 'text', 1506191745, 'raw')


@mock.patch('bottery.message.settings')
def test_render(mocked_settings):
    '''
    Test if a message template is rendered with a
    specified context.
    '''

    with tempfile.TemporaryDirectory() as tempdir:
        path = os.path.join(tempdir, 'templates')
        os.mkdir(path)

        template_path = os.path.join(path, 'template.md')
        with open(template_path, 'w') as template:
            template.write('{{ user }} {{ text }}!')

        mocked_settings.TEMPLATES = [path]
        rendered = render(message, 'template.md', {'text': 'rocks'})
        assert rendered == 'Bottery rocks!'


def test_datetime():