# coding: utf-8

from leancloud import Engine
from leancloud import LeanEngineError
import json

from app import app


engine = Engine(app)


@engine.define
def hello(**params):
    if 'name' in params:
        return 'Hello, {}!'.format(params['name'])
    else:
        return 'Hello, LeanCloud!'


@engine.before_save('Todo')
def before_todo_save(todo):
    content = todo.get('content')
    if not content:
        raise LeanEngineError('内容不能为空')
    if len(content) >= 240:
        todo.set('content', content[:240] + ' ...')

@engine.define
def _messageReceived(**params):
    # params = {
    #     'fromPeer': 'Tom',
    #     'receipt': false,
    #     'groupId': null,
    #     'system': null,
    #     'content': '{"_lctext":"耗子，起床！","_lctype":-1}',
    #     'convId': '5789a33a1b8694ad267d8040',
    #     'toPeers': ['Jerry'],
    #     '__sign': '1472200796787,a0e99be208c6bce92d516c10ff3f598de8f650b9',
    #     'bin': false,
    #     'transient': false,
    #     'sourceIP': '121.239.62.103',
    #     'timestamp': 1472200796764,
    # }
    print('_messageReceived start')
    content = json.loads(params['content'])
    print('text:', content)
    processed_content = '12345'
    print('_messageReceived end')
    # 必须含有以下语句给服务端一个正确的返回，否则会引起异常
    return {
        'content': processed_content,
    }


@@ -1055,16 +1055,252 @@
     # %7D%0A
+%09    # %E5%8F%AF%E4%BB%A5%E7%94%A8%E7%BB%A7%E6%89%BF%E7%9A%84%E6%96%B9%E5%BC%8F%E5%AE%9A%E4%B9%89 leancloud.Object %E7%9A%84%E5%AD%90%E7%B1%BB%0A%09class Todo2(leancloud.Object):%0A%09    pass%0A%09# %E6%88%96%E8%80%85%E7%94%A8%E4%BB%A5%E4%B8%8B%E7%9A%84%E6%96%B9%E5%BC%8F%E5%AE%9A%E4%B9%89%E5%AD%90%E7%B1%BB%0A%09# Todo = leancloud.Object.extend('Todo')%0A%09todo = Todo2()%0A%09todo.set('title', '%E5%B7%A5%E7%A8%8B%E5%B8%88%E5%91%A8%E4%BC%9A')%0A%09todo.set('content', '%E6%AF%8F%E5%91%A8%E5%B7%A5%E7%A8%8B%E5%B8%88%E4%BC%9A%E8%AE%AE%EF%BC%8C%E5%91%A8%E4%B8%80%E4%B8%8B%E5%8D%882%E7%82%B9')%0A%09todo.save()%0A%0A
     prin

添加todo2