Skip to content

Instantly share code, notes, and snippets.

@endofline
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endofline/900fa605636b87b56894 to your computer and use it in GitHub Desktop.
Save endofline/900fa605636b87b56894 to your computer and use it in GitHub Desktop.
Conversation Tools: Public Channels (Proof of Concept)
import hangups
import plugins
def _initialise(bot):
plugins.register_user_command(["public"])
def public(bot, event, *args):
"""list all available public channels when called without any parameters.
to join a channel, call this command with the desired channel number"""
channel_list =_get_public_channels(bot, event)
if len(args) == 0:
if len(channel_list) > 0:
lines = ["<b>Public channels:</b>"]
for i, meta in enumerate(channel_list):
lines.append("{}. {}".format(i + 1, meta[1]))
lines.append("<em>To add yourself to a channel, reply with <b>{} public (channel number)</b></em>".format(bot._handlers.bot_command[0]))
message = '<br />'.join(lines)
else:
message = "<i>no public channels available</i>"
bot.send_html_to_conversation(event.conv.id_, message)
else:
channels = []
try:
addto = list(set(args))
for number in addto:
channels.append(channel_list[int(number) - 1])
except (IndexError, ValueError) as e:
bot.send_html_to_conversation(event.conv.id_, "<em>channel {} does not exist</em>".format(number))
return
added = []
failed = []
for _cid, _cname in channels:
try:
yield from bot._client.adduser(_cid, [event.user.id_.chat_id])
added.append(_cname)
except hangups.exceptions.NetworkError:
failed.append(_cname)
lines = []
if added:
lines.append("You have been added to: {}".format(", ".join(added)))
if failed:
lines.append("Could not add you to: {} - you may have already been added before".format(", ".join(failed)))
message = "; ".join(lines)
bot.send_html_to_conversation(event.conv.id_, "<em>{}</em>".format(message))
def _get_public_channels(bot, event):
"""separated to allow different methods to retrieve a list of public channels, which might be
dependent on user filtering, context, etc"""
channel_list = []
public_channels = bot.get_config_suboption(event.conv.id_, 'public_channels')
if isinstance(public_channels, dict) and len(public_channels) > 0:
for _cid, _ctitle in public_channels.items():
channel_list.append([_cid, _ctitle])
return channel_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment