Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def handle_forward(self, event):
# Test if message forwarding is enabled
if not self.bot.get_config_suboption(event.conv_id, 'forwarding_enabled'):
return
forward_to_list = self.bot.get_config_suboption(event.conv_id, 'forward_to')
if forward_to_list:
for dst in forward_to_list:
try:
conv = self.bot._conv_list.get(dst)
except KeyError:
continue
# Prepend forwarded message with name of sender
link = 'https://plus.google.com/u/0/{}/about'.format(event.user_id.chat_id)
segments = [hangups.ChatMessageSegment(event.user.full_name, hangups.SegmentType.LINK,
link_target=link, is_bold=True),
hangups.ChatMessageSegment(': ', is_bold=True)]
# Copy original message segments
segments.extend(event.conv_event.segments)
# Append links to attachments (G+ photos) to forwarded message
if event.conv_event.attachments:
segments.append(hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK))
segments.extend([hangups.ChatMessageSegment(link, hangups.SegmentType.LINK, link_target=link)
for link in event.conv_event.attachments])
self.bot.send_message_segments(conv, segments)
# Generate list of added or removed users
event_users = [event.conv.get_user(user_id) for user_id
in event.conv_event.participant_ids]
names = ', '.join([user.full_name for user in event_users])
# JOIN
if event.conv_event.type_ == hangups.MembershipChangeType.JOIN:
# Test if user who added new participants is admin
admins_list = self.get_config_suboption(event.conv_id, 'admins')
if event.user_id.chat_id in admins_list:
self.send_message(event.conv,
'{}: Hello and welcome!'.format(names))
else:
segments = [hangups.ChatMessageSegment('!!! CAUTION !!!', is_bold=True),
hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK),
hangups.ChatMessageSegment('{} has illegally added {} to this hangout!'.format(
event.user.full_name, names)),
hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK),
hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK),
hangups.ChatMessageSegment('{}: Please leave this Hangout immediately!'.format(names))]
self.send_message_segments(event.conv, segments)
# LEAVE
else:
self.send_message(event.conv, 'Goodbye, {}.'.format(names))
summary = " ".join(summary.summaries[:3])
else:
summary = " ".join(summary.summaries)
except HTTPError as e:
segments = [hangups.ChatMessageSegment('"{}" gave HTTP error code {}.'.format(url, e.code))]
bot.send_message_segments(event.conv, segments)
return
except (ValueError, URLError):
yield from bot._client.settyping(event.conv_id, hangups.TypingStatus.STOPPED)
return
bot.send_message_segments(event.conv, [
hangups.ChatMessageSegment(summary),
hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK),
hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK),
hangups.ChatMessageSegment(url, hangups.SegmentType.LINK, link_target=url)])
# Test if message forwarding is enabled
if not bot.get_config_suboption(event.conv_id, 'forwarding_enabled'):
return
forward_to_list = bot.get_config_suboption(event.conv_id, 'forward_to')
if forward_to_list:
print(_("FORWARDING: {}").format(forward_to_list))
for dst in forward_to_list:
try:
conv = bot._conv_list.get(dst)
except KeyError:
continue
# Prepend forwarded message with name of sender
link = 'https://plus.google.com/u/0/{}/about'.format(event.user_id.chat_id)
segments = [hangups.ChatMessageSegment(event.user.full_name, hangups.SegmentType.LINK,
link_target=link, is_bold=True),
hangups.ChatMessageSegment(': ', is_bold=True)]
# Copy original message segments
segments.extend(event.conv_event.segments)
# Append links to attachments (G+ photos) to forwarded message
if event.conv_event.attachments:
segments.append(hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK))
segments.extend([hangups.ChatMessageSegment(link, hangups.SegmentType.LINK, link_target=link)
for link in event.conv_event.attachments])
bot.send_message_segments(conv, segments)
def send_goog_message(bot, event, url, query=None, headers=None):
req = request.Request(url, None, headers)
try:
resp = request.urlopen(req, timeout=10)
except HTTPError:
segments = [hangups.ChatMessageSegment('Result:', is_bold=True),
hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK)]
if query:
segments.append(hangups.ChatMessageSegment(query, hangups.SegmentType.LINK, link_target=url))
bot.send_message_segments(event.conv, segments)
return
soup = BeautifulSoup(resp)
bot.send_message_segments(event.conv, [hangups.ChatMessageSegment('Result:', is_bold=True),
hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK),
hangups.ChatMessageSegment(soup.title.string, hangups.SegmentType.LINK,
link_target=url)])
if origin_language != target_language:
logger.debug("translating {} to {}".format(origin_language, target_language))
translated = _autotranslate["event_text"]
try:
en_blob = TextBlob(_autotranslate["event_text"])
translated = "{0}".format(en_blob.translate(to=target_language))
#translated = gs.translate(_autotranslate["event_text"], target_language
except Exception:
logger.debug("Translation Api returned string unchanged")
else:
pass
finally:
if _autotranslate["event_text"] != translated:
# mutate the original response by reference
response.extend([
hangups.ChatMessageSegment('\n', hangups.SegmentType.LINE_BREAK),
hangups.ChatMessageSegment('(' + translated + ')')])
def segments_linebreak(self):
self._segments.append(
hangups.ChatMessageSegment(
"\n",
hangups.SegmentType.LINE_BREAK))
"""public api: http://version1.api.memegenerator.net"""
url_api = 'http://version1.api.memegenerator.net/Instances_Search?q=' + "+".join(parameters) + '&pageIndex=0&pageSize=25'
api_request = yield from aiohttp.request('get', url_api)
json_results = yield from api_request.read()
results = json.loads(str(json_results, 'utf-8'))
if len(results['result']) > 0:
instanceImageUrl = random.choice(results['result'])['instanceImageUrl']
req = urllib.request.Request(instanceImageUrl, headers={'User-Agent': 'Mozilla/5.0'})
image_data = urllib.request.urlopen(req)
filename = os.path.basename(instanceImageUrl)
legacy_segments = [hangups.ChatMessageSegment( instanceImageUrl,
hangups.SegmentType.LINK,
link_target = instanceImageUrl )]
logger.debug("uploading {} from {}".format(filename, instanceImageUrl))
try:
photo_id = yield from bot.call_shared('image_upload_single', instanceImageUrl)
except KeyError:
logger.warning('image plugin not loaded - using legacy code')
photo_id = yield from bot._client.upload_image(image_data, filename=filename)
yield from bot.coro_send_message(event.conv.id_, legacy_segments, image_id=photo_id)
else:
yield from bot.coro_send_message(event.conv_id, "<i>couldn't find a nice picture :( try again</i>")
except Exception as e:
yield from bot.coro_send_message(event.conv_id, "<i>couldn't find a suitable meme! try again</i>")