def test_get_context(): bot = Minette(prepare_table=True) with bot.connection_provider.get_connection() as connection: # register context for test ctx = bot.context_store.get( channel="get_context_test", channel_user_id=user_id, connection=connection) ctx.data["unixtime"] = date_to_unixtime(now) bot.context_store.save(ctx, connection) ctx_group = bot.context_store.get( channel="get_context_test", channel_user_id="group_" + user_id, connection=connection) ctx_group.data["unixtime"] = date_to_unixtime(now) bot.context_store.save(ctx_group, connection) ctx_detail = bot.context_store.get( channel="get_context_test_detail", channel_user_id=user_id, connection=connection) ctx_detail.data["unixtime"] = date_to_unixtime(now) bot.context_store.save(ctx_detail, connection) # without detail request = Message( text="hello", channel="get_context_test", channel_user_id=user_id) context = bot._get_context(request, connection) assert context.channel == "get_context_test" assert context.channel_user_id == user_id assert context.data["unixtime"] == date_to_unixtime(now) # group without group request = Message( text="hello", channel="get_context_test", channel_user_id=user_id) request.group = Group(id="group_" + user_id) context = bot._get_context(request, connection) assert context.channel == "get_context_test" assert context.channel_user_id == "group_" + user_id assert context.data["unixtime"] == date_to_unixtime(now) # with detail bot.config.confg_parser.set( "minette", "context_scope", "channel_detail") request = Message( text="hello", channel="get_context_test", channel_detail="detail", channel_user_id=user_id) context = bot._get_context(request, connection) assert context.channel == "get_context_test_detail" assert context.channel_user_id == user_id assert context.data["unixtime"] == date_to_unixtime(now)
def _to_minette_message(self, event): """ Convert Symphony Event object to Minette Message object Parameters ---------- event : Event Event from Symphony. See the url below for information https://developers.symphony.com/restapi/docs/real-time-events Returns ------- message : minette.Message Request message object """ if self.debug: self.logger.info(event) msg = Message(type=event["type"], channel="Symphony", channel_detail="", channel_user_id=str( event["initiator"]["user"]["userId"]), channel_message=event) if msg.type == "MESSAGESENT": m = event["payload"]["messageSent"]["message"] msg.token = m["stream"]["streamId"] msg.text = lxml.html.fromstring(m["message"]).text_content() if m["stream"]["streamType"] == "ROOM": msg.group = Group(id=m["stream"]["streamId"], type="ROOM") if "attachments" in m: for a in m["attachments"]: if len(a["images"]) > 0: content_type = "image" else: content_type = "file" msg.payloads.append( Payload(content_type=content_type, content=a)) return msg