def on_message(self, message): message = json.loads(message) Dialog.write_message(message, self.get_current_user()) dialog = message['dialog_id'] for key, value in enumerate(self.application.webSocketsPool): if value != self: if dialog in value._list_of_dialogs: value.ws_connection.write_message(message)
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)
def get(self): """ :returns all dialogs of current_user """ user = self.get_current_user() if not user: raise HTTPError(300, 'forbidden') res = yield Dialog.get_all_with_user(user) self.write(json.dumps(res))
def post(self): try: data = json.loads(self.request.body.decode()) except: raise name = data['name'] people = data['people'] res = yield Dialog.create(people, name) if res: self.write('ok') else: raise HTTPError(400, 'hz')
def dialogwith(userid): user = User.query.filter(User.id == userid).first() if not user: flash('There is no user with id == ' + str(userid)) return redirect('/users') q = finddialogbetween(current_user.id, user.id) if q.count() > 0: dialog = q.first() else: dialog = Dialog(current_user.id, user.id) db.session.add(dialog) db.session.commit() return render_template('dialogwith.html', title=user.username, user=user, dialog=dialog)
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()
def weichat(groupchoose): form = DialogForm() dialog_show = Dialog() info = dialog_show.show_group_dialog(groupchoose) my_chat = request.form.get('my_chat') if form.validate_on_submit(): my_chat = request.form.get('my_chat') time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') user = current_user.user_email dialog_new = Dialog() dialog_new.create_dialog(groupchoose, user, time, my_chat) return redirect( request.args.get('next') or url_for('weichat', groupchoose=groupchoose)) return render_template('weichat.html', name=groupchoose, msgs=info, form=form, user_info_global=user_info_global)
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()
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)
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)
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)
def save_dialog(content, reply, fowler): dialog = Dialog(message=content, reply=reply, fowler=fowler) dialog.save()
def open(self): self.application.webSocketsPool.append(self) user = self.get_current_user() lst = yield Dialog.get_all_with_user(user) lst = [d['id'] for d in lst] self._list_of_dialogs = lst