Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
image_id = info['image_id']
context = {
"parser": False,
}
msg1 = [
ChatMessageSegment("xkcd #%s: " % info['num']),
ChatMessageSegment(info["title"], is_bold=True),
]
msg2 = [
ChatMessageSegment(info["alt"]),
] + ChatMessageSegment.from_str('<br>- <i><a href="https://xkcd.com/%s">CC-BY-SA xkcd</a></i>' % info['num'])
if "link" in info and info["link"]:
msg2.extend(ChatMessageSegment.from_str("<br>* see also %s" % info["link"]))
yield from bot.coro_send_message(event.conv.id_, msg1, context)
yield from bot.coro_send_message(event.conv.id_, msg2, context, image_id=image_id) # image appears above text, so order is [msg1, image, msg2]
if conversation is not None:
conversations.append(conversation)
if not conversations:
return False
messages = []
for segment in message:
if messages:
messages.append(
ChatMessageSegment(
"", segment_type=hangouts_pb2.SEGMENT_TYPE_LINE_BREAK
)
)
if "parse_str" in segment and segment["parse_str"]:
messages.extend(ChatMessageSegment.from_str(segment["text"]))
else:
if "parse_str" in segment:
del segment["parse_str"]
messages.append(ChatMessageSegment(**segment))
image_file = None
if data:
if data.get("image_url"):
uri = data.get("image_url")
try:
websession = async_get_clientsession(self.hass)
async with websession.get(uri, timeout=5) as response:
if response.status != 200:
_LOGGER.error(
"Fetch image failed, %s, %s", response.status, response
)
info['image_id'] = yield from bot._client.upload_image(image_data, filename=filename)
_cache[info['num']] = info
image_id = info['image_id']
context = {
"parser": False,
}
msg1 = [
ChatMessageSegment("xkcd #%s: " % info['num']),
ChatMessageSegment(info["title"], is_bold=True),
]
msg2 = [
ChatMessageSegment(info["alt"]),
] + ChatMessageSegment.from_str('<br>- <i><a href="https://xkcd.com/%s">CC-BY-SA xkcd</a></i>' % info['num'])
if "link" in info and info["link"]:
msg2.extend(ChatMessageSegment.from_str("<br>* see also %s" % info["link"]))
yield from bot.coro_send_message(event.conv.id_, msg1, context)
yield from bot.coro_send_message(event.conv.id_, msg2, context, image_id=image_id) # image appears above text, so order is [msg1, image, msg2]
def text_to_segments(text):
"""Create list of ChatMessageSegments from text"""
return hangups.ChatMessageSegment.from_str(text)
def text_to_segments(text):
"""Create list of message segments from text"""
return ChatMessageSegment.from_str(text)
#hangups.client.
#print(json.dumps({"cmd":"status", "status":"ready"}))
#print(hangups.user.)
while 1:
try:
msgtxt = yield from async_input()
msgtxt = msgtxt.strip()
# TOOD: take conversation id from other side
msgJson = json.loads(msgtxt)
conv = conv_list.get(msgJson['conversation_id'])
#print("ok, I got: " + msgtxt)
if msgJson['cmd'] == 'sendmessage':
segments = hangups.ChatMessageSegment.from_str(msgJson['msgbody'])
asyncio.ensure_future(
conv.send_message(segments)
).add_done_callback(_on_message_sent)
elif msgJson['cmd'] == 'sendimage':
segments = []
image_file = yield from download_image(msgJson['url'])
# deduplicate
image_file.name += '_mx_' + MIME_EXT.get(msgJson['mimetype'], '.unk')
if not image_file:
raise Exception("Invalid image url")
asyncio.ensure_future(
conv.send_message(segments, image_file=image_file)
).add_done_callback(_on_message_sent)
else:
raise Exception("Invalid cmd specified!")
def simple_parse_to_segments(formatted_text):
"""send formatted chat message
legacy notice: identical function in kludgy_html_parser
the older function is "overridden" here for compatibility reasons
"""
if "message_parser" in dir(hangups):
# ReParser is available in hangups 201504200224 (ae59c24) onwards
# supports html, markdown
segments = hangups.ChatMessageSegment.from_str(formatted_text)
else:
# fallback to internal parser
# supports html
segments = kludgy_html_parser.simple_parse_to_segments(formatted_text)
return segments