Beispiel #1
0
def process_client_share(records_buffer, username, target_handle):

    jrecord = json.loads(records_buffer)
    logger.debug("jsons list: "+ str(jrecord))

    record = Dialog(handle=target_handle)

    record.content = safe_attr(jrecord, 'content')
    logger.debug('record username: '******'sender')
    record.link = safe_attr(jrecord, 'link')
    record.create_date = get_date(safe_attr(jrecord, 'date'))
    record.create_time = safe_attr(jrecord, 'time')
    record.content_type = safe_attr(jrecord, 'ctx')
    record.photo = safe_attr(jrecord, 'po')
    record.audio = safe_attr(jrecord, 'ao')
    record.deleted = (safe_attr(jrecord, 'del') == 1)

    record.save()
    logger.debug('Saved record: '+record.handle)
    push_data = {}
    push_data['sender'] = username
    push_data['linker'] = target_handle
    push_data['id'] = record.id
    jpush_send_message(push_data, target_handle, 1001)
Beispiel #2
0
    def post(self, request):
        try:
            wechat.parse_data(request.body)
        except ParseError:
            return HttpResponse('Invalid Body Text')

        id = wechat.message.id              # MsgId
        target = wechat.message.target      # ToUserName
        source = wechat.message.source      # FromUserName
        time = wechat.message.time          # CreateTime
        type = wechat.message.type          # MsgType
        raw = wechat.message.raw            # 原始 XML 文本

        # get_or_create会得到一个tuple (object, created)
        fowler = Fowler.objects.get_or_create(OpenID=source)[0]

        if isinstance(wechat.message, TextMessage):
            keywords = [func.keyword for func in Function.objects.all()]
            content = wechat.message.content                # 对应于 XML 中的 Content
            if content in keywords:
                reply = Function.objects.get(keyword=content).explain
            else:
                reply = '本公众号支持的回复有: \n' + ' '.join(keywords)

            dialog = Dialog(message=content, reply=reply, fowler=fowler)
            dialog.save()
            response_xml = wechat.response_text(content=reply, escape=True)
            return HttpResponse(response_xml)

        elif isinstance(wechat.message, LocationMessage):
            location = wechat.message.location              # Tuple(Location_X, Location_Y)
            scale = wechat.message.scale                    # 地图缩放大小
            label = wechat.message.label                    # 地理位置

            loc = Location(fowler=fowler, x=location[0], y=location[1], label=label)
            loc.save()
            response_xml = wechat.response_text(content='已收到您的地理位置')
            return HttpResponse(response_xml)

        elif isinstance(wechat.message, EventMessage):
            if wechat.message.type == 'subscribe':
                fowler.activate = 1
                fowler.save()
                response_xml = wechat.response_text(content='欢迎关注本公众号 具体功能请回复‘功能’')
                return HttpResponse(response_xml)
            elif wechat.message.type == 'unsubscribe':
                fowler.activate = 0
                fowler.save()
        else:
            response_xml = wechat.response_text(content="回复'功能'了解本公众号提供的查询功能")
            return HttpResponse(response_xml)
Beispiel #3
0
	def setUp(self):
		record1 = Dialog(handle='temp',
			content="what's thsis",
			create_date=timezone.now(),
			create_time=timezone.now(),
			content_type=1,
			link="temp",
			deleted=False)
		record1.save()

		record2 = Dialog(handle='temp',
			content="time filping",
			create_date=timezone.now(),
			create_time=timezone.now(),
			content_type=1,
			link="temp",
			deleted=False)
		record2.save()
Beispiel #4
0
def run(request):
    dialog_key = request.POST.get("dialog_key", None)
    if dialog_key is None:
        task_id = request.POST.get("task_id", None)  # If dialog key is None.

        # Check API key.
        try:
            team = Team.objects.get(key=request.POST["key"])
        except Exception, e:
            return json_response({'status': 0, 'error': "Unknown API key, please check it again."})

        # Initialize the dialog.
        dlg = Dialog()
        dlg.task_id = task_id
        dlg.team = team
        dlg.dialog_key = id_generator(16)
        dlg.dialog_key2 = id_generator(16)
        dlg.system_id = request.POST.get("system_id", None)
        dlg.save()
Beispiel #5
0
def process_client_anonymous_share(records_buffer, username):

    jrecord = json.loads(records_buffer)
    logger.debug("jsons list: "+ str(jrecord))
    #select random target handle : not user or user's friends
    totalIds = User.objects.all().count() - 1
    if totalIds <= 1:
        return

    randomId = randint(0, totalIds)
    others = User.objects.get(id=randomId)
    if others.username == username or others.username == 'root':
        randomId = randint(0, totalIds)
        others = User.objects.get(id=randomId)
    target_handle = others.username
    logger.debug('target handle: ' + target_handle)

    record = Dialog(handle=target_handle)

    record.content = safe_attr(jrecord, 'content')
    logger.debug('record context: ' + record.content)
    logger.debug('record username: '******'sender')
    record.link = others.username
    record.create_date = get_date(safe_attr(jrecord, 'date'))
    record.create_time = safe_attr(jrecord, 'time')
    record.content_type = safe_attr(jrecord, 'ctx')
    record.photo = safe_attr(jrecord, 'po')
    record.audio = safe_attr(jrecord, 'ao')
    record.deleted = (safe_attr(jrecord, 'del') == 1)

    record.save()
    logger.debug('Saved record: '+record.handle)
    push_data = {}
    push_data['username'] = username
    push_date['id'] = record.id
    jpush_send_message(toJSON(push_data), target_handle, 1001)
Beispiel #6
0
def resetdb(request):
	records = Dialog.objects.all()
	for record in records:
		record.delete()

	record1 = Dialog(handle='temp',
		content="what's thsis",
		create_date=timezone.now(),
		create_time=timezone.now(),
		content_type=1,
		link="temp",
		deleted=False)
	record1.save()

	record2 = Dialog(handle='temp',
		content="time filping",
		create_date=timezone.now(),
		create_time=timezone.now(),
		content_type=1,
		link="temp",
		deleted=False)
	record2.save()

	return HttpResponse(200)
Beispiel #7
0
 def save_dialog(content, reply, fowler):
     dialog = Dialog(message=content, reply=reply, fowler=fowler)
     dialog.save()