def always(self, m): if str(m.sender.id).startswith('-100'): return # Update group data # gid = str(m.conversation.id) if m.conversation.id < 0: if self.bot.groups: if gid in self.bot.groups: self.bot.groups[gid].title = m.conversation.title self.bot.groups[gid].messages += 1 else: self.bot.groups[gid] = { "title": m.conversation.title, "messages": 1 } set_data('groups/%s/%s' % (self.bot.name, gid), self.bot.groups[gid]) # Update user data # uid = str(m.sender.id) if self.bot.users: if uid in self.bot.users: self.bot.users[uid].first_name = m.sender.first_name if hasattr(m.sender, 'last_name'): self.bot.users[uid].last_name = m.sender.last_name if hasattr(m.sender, 'username'): self.bot.users[uid].username = m.sender.username self.bot.users[uid].messages += 1 else: self.bot.users[uid] = { "first_name": m.sender.first_name, "last_name": m.sender.last_name, "username": m.sender.username, "messages": 1 } set_data('users/%s/%s' % (self.bot.name, uid), self.bot.users[uid]) # Update group id when upgraded to supergroup # if m.type == 'notification' and m.content == 'upgrade_to_supergroup': to_id = str(m.extra['chat_id']) from_id = str(m.extra['from_chat_id']) if from_id in self.bot.administration: self.bot.administration[to_id] = self.bot.administration[ from_id] del self.bot.administration[from_id] set_data('administration/%s/%s' % (self.bot.name, to_id), self.bot.administration[to_id]) delete_data('administration/%s/%s' % (self.bot.name, from_id)) self.bot.groups[to_id] = self.bot.groups[from_id] del self.bot.groups[from_id] set_data('groups/%s/%s' % (self.bot.name, to_id), self.bot.groups[to_id]) delete_data('groups/%s/%s' % (self.bot.name, from_id))
def always(self, m): # Update group id when upgraded to supergroup # if m.type == 'notification' and m.content == 'upgrade_to_supergroup': to_id = str(m.extra['chat_id']) from_id = str(m.extra['from_chat_id']) if from_id in self.bot.administration: self.bot.administration[to_id] = self.bot.administration[from_id] del self.bot.administration[from_id] set_data('administration/%s/%s' % (self.bot.name, to_id), self.bot.administration[to_id]) delete_data('administration/%s/%s' % (self.bot.name, from_id)) self.bot.groups[to_id] = self.bot.groups[from_id] del self.bot.groups[from_id] set_data('groups/%s/%s' % (self.bot.name, to_id), self.bot.groups[to_id]) delete_data('groups/%s/%s' % (self.bot.name, from_id))
def request_processing(self, request, response): leave_list = [ 'no rights', 'no write access', 'not enough rights to send', 'need administrator rights', 'channel_private' ] other_error = True for term in leave_list: if term in response['message'].lower() and not has_tag( self.bot, request['chat_id'], 'resend:?') and not has_tag( self.bot, request['chat_id'], 'fwd:?'): self.bot.send_admin_alert('Leaving chat: {} [{}]'.format( self.bot.groups[str(request['chat_id'])].title, request['chat_id'])) res = self.bot.bindings.kick_conversation_member( request['chat_id'], self.bot.info.id) other_error = False break if response['message'].lower() == 'invalid remote id': pins = self.bot.pins.copy() for pin in pins: if 'content' in self.bot.pins[pin] and 'type' in self.bot.pins[ pin] and self.bot.pins[pin]['type'] in request[ 'input_message_content'] and self.bot.pins[pin][ 'content'] == request['input_message_content'][ self.bot.pins[pin]['type']]['id']: if pin in self.bot.pins: self.bot.send_admin_alert( 'Deleting invalid pin: {} [{}]'.format( pin, self.bot.pins[pin]['content'])) delete_data('pins/%s/%s' % (self.bot.name, pin)) del self.bot.pins[pin] other_error = False elif response['message'].lower() == 'chat not found': logging.info('Chat not found: {}'.format(request['chat_id'])) other_error = False if other_error: self.bot.send_alert(request) self.bot.send_alert(response)
def run(self, m): if str(m.sender.id).startswith('-100'): return if m.conversation.id > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) if has_tag(self.bot, m.conversation.id, 'nopole'): return gid = str(m.conversation.id) uid = m.sender.id now = datetime.now().replace(microsecond=0) date = now.isoformat().split('T')[0] text = None # Pole ranking if is_command(self, 1, m.content): if time_in_range(time(1, 0, 0), time(2, 0, 0), now.time()): type = 1 elif time_in_range(time(12, 0, 0), time(13, 0, 0), now.time()): type = 2 else: type = 0 if gid in self.bot.poles: ranking = DictObject() for day in self.bot.poles[gid]: if type == 0: if 'pole' in self.bot.poles[gid][day]: try: ranking[str( self.bot.poles[gid][day].pole)].p += 1 except: ranking[str(self.bot.poles[gid][day].pole)] = { 'p': 1, 's': 0, 'f': 0, 'i': 0 } if 'subpole' in self.bot.poles[gid][day]: try: ranking[str( self.bot.poles[gid][day].subpole)].s += 1 except: ranking[str( self.bot.poles[gid][day].subpole)] = { 'p': 0, 's': 1, 'f': 0, 'i': 0 } if 'fail' in self.bot.poles[gid][day]: try: ranking[str( self.bot.poles[gid][day].fail)].f += 1 except: ranking[str(self.bot.poles[gid][day].fail)] = { 'p': 0, 's': 0, 'f': 1, 'i': 0 } if 'iron' in self.bot.poles[gid][day]: try: ranking[str( self.bot.poles[gid][day].iron)].i += 1 except: ranking[str(self.bot.poles[gid][day].iron)] = { 'p': 0, 's': 0, 'f': 0, 'i': 1 } if type == 1: if 'canaria' in self.bot.poles[gid][day]: try: ranking[str( self.bot.poles[gid][day].canaria)].c += 1 except: ranking[str( self.bot.poles[gid][day].canaria)] = { 'c': 1 } if type == 2: if 'andaluza' in self.bot.poles[gid][day]: try: ranking[str( self.bot.poles[gid][day].andaluza)].a += 1 except: ranking[str( self.bot.poles[gid][day].andaluza)] = { 'a': 1 } text = '' if type == 0: text = self.bot.trans.plugins.pole.strings.ranking for uid, values in self.order_by_points(ranking, type): points = str((values.p * 3) + (values.s * 1) + (values.f * 0.5) + (values.i * 0.1)).rstrip('0').rstrip('.') text += '\n ' + self.bot.trans.plugins.pole.strings.ranking_points % ( self.bot.users[uid].first_name, points) poles = '\n\n' + self.bot.trans.plugins.pole.strings.poles poles_empty = True for uid, values in self.order_by_poles(ranking): if values.p: poles_empty = False poles += '\n ' + self.bot.trans.plugins.pole.strings.ranking_poles % ( self.bot.users[uid].first_name, values.p) if not poles_empty: text += poles subpoles = '\n\n' + self.bot.trans.plugins.pole.strings.subpoles subpoles_empty = True for uid, values in self.order_by_subpoles(ranking): if values.s: subpoles_empty = False subpoles += '\n ' + self.bot.trans.plugins.pole.strings.ranking_subpoles % ( self.bot.users[uid].first_name, values.s) if not subpoles_empty: text += subpoles fails = '\n\n' + self.bot.trans.plugins.pole.strings.fails fails_empty = True for uid, values in self.order_by_fails(ranking): if values.f: fails_empty = False fails += '\n ' + self.bot.trans.plugins.pole.strings.ranking_fails % ( self.bot.users[uid].first_name, values.f) if not fails_empty: text += fails irons = '\n\n' + self.bot.trans.plugins.pole.strings.irons irons_empty = True for uid, values in self.order_by_irons(ranking): if values.i: irons_empty = False irons += '\n ' + self.bot.trans.plugins.pole.strings.ranking_irons % ( self.bot.users[uid].first_name, values.i) if not irons_empty: text += irons elif type == 1: poles_canarias = '\n\n' + self.bot.trans.plugins.pole.strings.poles_canarias poles_canarias_empty = True for uid, values in self.order_by_poles_canarias(ranking): if values.c: poles_canarias_empty = False poles_canarias += '\n ' + \ self.bot.trans.plugins.pole.strings.ranking_poles % ( self.bot.users[uid].first_name, values.c) if not poles_canarias_empty: text += poles_canarias elif type == 2: poles_andaluzas = '\n\n' + self.bot.trans.plugins.pole.strings.poles_andaluzas poles_andaluzas_empty = True for uid, values in self.order_by_poles_andaluzas(ranking): if values.a: poles_andaluzas_empty = False poles_andaluzas += '\n ' + \ self.bot.trans.plugins.pole.strings.ranking_poles % ( self.bot.users[uid].first_name, values.a) if not poles_andaluzas_empty: text += poles_andaluzas # Pole elif is_command(self, 2, m.content): if self.has_pole(gid, uid, date): return if not gid in self.bot.poles: self.bot.poles[gid] = DictObject({date: {'pole': uid}}) else: if not date in self.bot.poles[gid]: self.bot.poles[gid][date] = DictObject() if not 'pole' in self.bot.poles[gid][date]: self.bot.poles[gid][date].pole = uid else: return set_data('poles/%s/%s/%s' % (self.bot.name, gid, date), self.bot.poles[gid][date]) uid = str(uid) user = '' if 'first_name' in self.bot.users[uid]: user += self.bot.users[uid]['first_name'] if 'username' in self.bot.users[uid] and self.bot.users[ uid].username: user = '******' + self.bot.users[uid].username text = self.bot.trans.plugins.pole.strings.got_pole % user # Subole elif is_command(self, 3, m.content): if self.has_pole(gid, uid, date): return if not gid in self.bot.poles: self.bot.poles[gid] = DictObject({date: {'subpole': uid}}) else: if not date in self.bot.poles[gid]: self.bot.poles[gid][date] = DictObject() if not 'pole' in self.bot.poles[gid][date]: uid = str(uid) text = self.bot.trans.plugins.pole.strings.too_soon % self.bot.users[ uid].first_name return self.bot.send_message(m, text, extra={'format': 'HTML'}) elif not 'subpole' in self.bot.poles[gid][date]: self.bot.poles[gid][date].subpole = uid else: return set_data('poles/%s/%s/%s' % (self.bot.name, gid, date), self.bot.poles[gid][date]) uid = str(uid) user = '' if 'first_name' in self.bot.users[uid]: user += self.bot.users[uid]['first_name'] if 'username' in self.bot.users[uid] and self.bot.users[ uid].username: user = '******' + self.bot.users[uid].username text = self.bot.trans.plugins.pole.strings.got_subpole % user # Fail elif is_command(self, 4, m.content): if self.has_pole(gid, uid, date): return if not gid in self.bot.poles: self.bot.poles[gid] = DictObject({date: {'fail': uid}}) else: if not date in self.bot.poles[gid]: self.bot.poles[gid][date] = DictObject() if not 'pole' in self.bot.poles[gid][ date] or not 'subpole' in self.bot.poles[gid][date]: uid = str(uid) text = self.bot.trans.plugins.pole.strings.too_soon % self.bot.users[ uid].first_name return self.bot.send_message(m, text, extra={'format': 'HTML'}) elif not 'fail' in self.bot.poles[gid][date]: self.bot.poles[gid][date].fail = uid else: return set_data('poles/%s/%s/%s' % (self.bot.name, gid, date), self.bot.poles[gid][date]) uid = str(uid) user = '' if 'first_name' in self.bot.users[uid]: user += self.bot.users[uid]['first_name'] if 'username' in self.bot.users[uid] and self.bot.users[ uid].username: user = '******' + self.bot.users[uid].username text = self.bot.trans.plugins.pole.strings.got_fail % user # Pole canaria elif is_command(self, 5, m.content): if self.has_pole(gid, uid, date, 1): return if not time_in_range(time(1, 0, 0), time(2, 0, 0), now.time()): return if not gid in self.bot.poles: self.bot.poles[gid] = DictObject({date: {'canaria': uid}}) else: if not date in self.bot.poles[gid]: self.bot.poles[gid][date] = DictObject() if not 'canaria' in self.bot.poles[gid][date]: self.bot.poles[gid][date].canaria = uid else: return set_data('poles/%s/%s/%s' % (self.bot.name, gid, date), self.bot.poles[gid][date]) uid = str(uid) user = '' if 'first_name' in self.bot.users[uid]: user += self.bot.users[uid]['first_name'] if 'username' in self.bot.users[uid] and self.bot.users[ uid].username: user = '******' + self.bot.users[uid].username text = self.bot.trans.plugins.pole.strings.got_pole_canaria % user # Pole andaluza elif is_command(self, 6, m.content): if self.has_pole(gid, uid, date, 1): return if not time_in_range(time(12, 0, 0), time(13, 0, 0), now.time()): uid = str(uid) text = self.bot.trans.plugins.pole.strings.too_soon_for_andaluza % self.bot.users[ uid].first_name return self.bot.send_message(m, text, extra={'format': 'HTML'}) if not gid in self.bot.poles: self.bot.poles[gid] = DictObject({date: {'andaluza': uid}}) else: if not date in self.bot.poles[gid]: self.bot.poles[gid][date] = DictObject() if not 'andaluza' in self.bot.poles[gid][date]: self.bot.poles[gid][date].andaluza = uid else: return set_data('poles/%s/%s/%s' % (self.bot.name, gid, date), self.bot.poles[gid][date]) uid = str(uid) user = '' if 'first_name' in self.bot.users[uid]: user += self.bot.users[uid]['first_name'] if 'username' in self.bot.users[uid] and self.bot.users[ uid].username: user = '******' + self.bot.users[uid].username text = self.bot.trans.plugins.pole.strings.got_pole_andaluza % user # Hierro elif is_command(self, 7, m.content): if self.has_pole(gid, uid, date): return if not gid in self.bot.poles: self.bot.poles[gid] = DictObject({date: {'iron': uid}}) else: if not date in self.bot.poles[gid]: self.bot.poles[gid][date] = DictObject() if not 'pole' in self.bot.poles[gid][ date] or not 'subpole' in self.bot.poles[gid][ date] or not 'fail' in self.bot.poles[gid][date]: uid = str(uid) text = self.bot.trans.plugins.pole.strings.too_soon % self.bot.users[ uid].first_name return self.bot.send_message(m, text, extra={'format': 'HTML'}) elif not 'iron' in self.bot.poles[gid][date]: self.bot.poles[gid][date].iron = uid else: return set_data('poles/%s/%s/%s' % (self.bot.name, gid, date), self.bot.poles[gid][date]) uid = str(uid) user = '' if 'first_name' in self.bot.users[uid]: user += self.bot.users[uid]['first_name'] if 'username' in self.bot.users[uid] and self.bot.users[ uid].username: user = '******' + self.bot.users[uid].username text = self.bot.trans.plugins.pole.strings.got_iron % user # Pole reset elif is_command(self, 8, m.content): if has_tag(self.bot, m.conversation.id, 'polereset'): if is_trusted(self.bot, m.sender.id, m): delete_data('poles/%s/%s' % (self.bot.name, gid)) self.bot.poles.pop(gid, None) text = self.bot.trans.plugins.pole.strings.polereset_done else: text = self.bot.trans.errors.admin_required else: text = self.bot.trans.plugins.config.strings.disabled if text: self.bot.send_message(m, text, extra={'format': 'HTML'})
def run(self, m): input = get_input(m) uid = str(m.sender.id) gid = str(m.conversation.id) # List all administration commands. # if is_command(self, 1, m.content): text = self.bot.trans.plugins.administration.strings.commands for command in self.commands: # Adds the command and parameters# if command.command == '^new_chat_member$': text += '\n • ' + \ self.bot.trans.plugins.administration.strings.new_chat_member else: text += '\n • ' + \ command.command.replace('/', self.bot.config.prefix) if 'parameters' in command and command.parameters: for parameter in command.parameters: name, required = list(parameter.items())[0] # Bold for required parameters, and italic for optional # if required: text += ' <b><%s></b>' % name else: text += ' [%s]' % name if 'description' in command: text += '\n <i>%s</i>' % command.description else: text += '\n <i>?¿</i>' return self.bot.send_message(m, text, extra={'format': 'HTML'}) # List all groups. # if is_command(self, 2, m.content): text = self.bot.trans.plugins.administration.strings.groups if len(self.bot.administration) > 0: for gid, attr in self.bot.administration.items(): if 'public' in self.bot.administration[ gid] and self.bot.administration[gid].public: if 'link' in self.bot.administration[ gid] and self.bot.config.bindings != 'telegram-cli': text += '\n • <a href="%s">%s</a>' % ( self.bot.administration[gid].link, self.bot.groups[gid].title) else: text += '\n • %s' % self.bot.groups[gid].title if self.bot.administration[gid].alias: text += ' /<b>%s</b>/' % attr.alias else: text = self.bot.trans.plugins.administration.strings.no_groups return self.bot.send_message(m, text, extra={ 'format': 'HTML', 'preview': False }) # Join a group. # elif is_command(self, 3, m.content): if not input: return self.bot.send_message( m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'}) for id in self.bot.administration: if (input in self.bot.administration or compile('^' + input.lower() + '$').search( self.bot.administration[id].alias.lower()) or compile('^' + input.lower() + '$').search( self.bot.groups[id].title.lower())): gid_to_join = id break if not gid_to_join in self.bot.administration: return self.bot.send_message( m, self.bot.trans.plugins.administration.strings.not_added % m.conversation.title, extra={'format': 'HTML'}) text = '<b>%s</b>' % self.bot.groups[gid_to_join].title if self.bot.administration[gid_to_join].motd: text += '\n<i>%s</i>' % self.bot.administration[ gid_to_join].motd text += '\n\n%s' % self.bot.trans.plugins.administration.strings.rules i = 1 if 'rules' in self.bot.administration[gid_to_join]: for rule in self.bot.administration[gid_to_join].rules: text += '\n %s. <i>%s</i>' % (i, rule) i += 1 else: text += '\n%s' % self.bot.trans.plugins.administration.strings.norules # if self.bot.administration[gid_to_join].public: # text += '\n\n%s' % self.bot.trans.plugins.administration.strings.public_group # else: # text += '\n\n%s' % self.bot.trans.plugins.administration.strings.private_group if not self.bot.administration[gid_to_join].link: text += '\n\n%s' % self.bot.trans.plugins.administration.strings.nolink else: text += '\n\n<a href="%s">%s</a>' % ( self.bot.administration[gid_to_join].link, self.bot.trans.plugins.administration.strings.join) return self.bot.send_message(m, text, extra={ 'format': 'HTML', 'preview': False }) # Information about a group. # elif is_command(self, 4, m.content) or is_command(self, 9, m.content): if m.conversation.id > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) if not gid in self.bot.administration: if is_command(self, 4, m.content): return self.bot.send_message( m, self.bot.trans.plugins.administration.strings.not_added % m.conversation.title, extra={'format': 'HTML'}) elif is_command(self, 9, m.content): return text = '<b>%s</b>' % self.bot.groups[gid].title if self.bot.administration[gid].motd: text += '\n<i>%s</i>' % self.bot.administration[gid].motd text += '\n\n%s' % self.bot.trans.plugins.administration.strings.rules i = 1 for rule in self.bot.administration[gid].rules: text += '\n %s. <i>%s</i>' % (i, rule) i += 1 if not self.bot.administration[gid].rules: text += '\n%s' % self.bot.trans.plugins.administration.strings.norules # if self.bot.administration[gid].public: # text += '\n\n%s' % self.bot.trans.plugins.administration.strings.public_group # else: # text += '\n\n%s' % self.bot.trans.plugins.administration.strings.private_group if is_command(self, 4, m.content): if not self.bot.administration[gid].link: text += '\n\n%s' % self.bot.trans.plugins.administration.strings.nolink else: text += '\n\n<a href="%s">%s</a>' % ( self.bot.trans.plugins.administration.strings.join, self.bot.administration[gid].link) return self.bot.send_message(m, text, extra={ 'format': 'HTML', 'preview': False }) # Rules of a group. # elif is_command(self, 5, m.content): if m.conversation.id > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) if not gid in self.bot.administration: return self.bot.send_message( m, self.bot.trans.plugins.administration.strings.not_added % m.conversation.title, extra={'format': 'HTML'}) if input and is_int(input): try: i = int(input) text = '%s. <i>%s</i>' % ( i, self.bot.administration[gid].rules[i - 1]) except: text = self.bot.trans.plugins.administration.strings.notfound else: text = self.bot.trans.plugins.administration.strings.rules i = 1 for rule in self.bot.administration[gid].rules: text += '\n %s. <i>%s</i>' % (i, rule) i += 1 if not self.bot.administration[gid].rules: text += '\n%s' % self.bot.trans.plugins.administration.strings.norules return self.bot.send_message(m, text, extra={ 'format': 'HTML', 'preview': False }) # Kicks a user. # elif is_command(self, 6, m.content): if m.conversation.id > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) if not is_mod(self.bot, m.sender.id, m.conversation.id): return self.bot.send_message( m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'}) if not input and not m.reply: return self.bot.send_message( m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'}) if m.reply: target = m.reply.sender.id elif is_int(input): target = input else: target = m.sender.id res = self.bot.kick_user(m, target) self.bot.unban_user(m, target) if res is None: return self.bot.send_message( m, self.bot.trans.errors.admin_required, extra={'format': 'HTML'}) elif not res: return self.bot.send_message(m, self.bot.trans.errors.failed, extra={'format': 'HTML'}) else: return self.bot.send_message( m, '<pre>An enemy has been slain.</pre>', extra={'format': 'HTML'}) return self.bot.send_message(m, self.bot.trans.errors.unknown, extra={'format': 'HTML'}) # Bans a user. # elif is_command(self, 7, m.content): if m.conversation.id > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) if not is_mod(self.bot, m.sender.id, m.conversation.id): return self.bot.send_message( m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'}) if not input: return self.bot.send_message( m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'}) return self.bot.send_message(m, self.bot.trans.errors.unknown, extra={'format': 'HTML'}) # Configures a group. # elif is_command(self, 8, m.content): if m.conversation.id > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) if not is_mod(self.bot, m.sender.id, m.conversation.id): return self.bot.send_message( m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'}) if not input: self.bot.send_message( m, self.bot.trans.plugins.administration.strings.ask_to_add % m.conversation.title, extra={ 'format': 'HTML', 'force_reply': True }) if not gid in self.bot.administration: self.bot.administration[gid] = { 'alias': None, 'description': None, 'link': None, 'rules': [], 'public': False } set_data('administration/%s/%s' % (self.bot.name, gid), self.bot.administration[gid]) set_step(self.bot, m.conversation.id, get_plugin_name(self), 1) else: if first_word(input) == 'add': if not gid in self.bot.administration: self.bot.administration[gid] = { 'alias': None, 'description': None, 'link': None, 'rules': [], 'public': False } set_data('administration/%s/%s' % (self.bot.name, gid), self.bot.administration[gid]) return self.bot.send_message( m, self.bot.trans.plugins.administration.strings.added % m.conversation.title, extra={'format': 'HTML'}) else: return self.bot.send_message( m, self.bot.trans.plugins.administration.strings. already_added % m.conversation.title, extra={'format': 'HTML'}) elif first_word(input) == 'remove': if gid in self.bot.administration: del self.bot.administration[gid] delete_data('administration/%s/%s' % (self.bot.name, gid)) return self.bot.send_message( m, self.bot.trans.plugins.administration.strings. removed % m.conversation.title, extra={'format': 'HTML'}) else: return self.bot.send_message( m, self.bot.trans.plugins.administration.strings. not_added % m.conversation.title, extra={'format': 'HTML'}) elif first_word(input) == 'alias': if gid in self.bot.administration: self.bot.administration[ gid].alias = all_but_first_word(input).lower() set_data( 'administration/%s/%s/alias' % (self.bot.name, gid), self.bot.administration[gid].alias) return self.bot.send_message( m, self.bot.trans.plugins.administration.strings.set % m.conversation.title, extra={'format': 'HTML'}) else: return self.bot.send_message( m, self.bot.trans.plugins.administration.strings. not_added % m.conversation.title, extra={'format': 'HTML'}) elif first_word(input) == 'motd': if gid in self.bot.administration: self.bot.administration[gid].motd = all_but_first_word( input) set_data( 'administration/%s/%s/motd' % (self.bot.name, gid), self.bot.administration[gid].motd) return self.bot.send_message( m, self.bot.trans.plugins.administration.strings.set % m.conversation.title, extra={'format': 'HTML'}) else: return self.bot.send_message( m, self.bot.trans.plugins.administration.strings. not_added % m.conversation.title, extra={'format': 'HTML'}) elif first_word(input) == 'link': if gid in self.bot.administration: self.bot.administration[gid].link = all_but_first_word( input) set_data( 'administration/%s/%s/link' % (self.bot.name, gid), self.bot.administration[gid].link) return self.bot.send_message( m, self.bot.trans.plugins.administration.strings.set % m.conversation.title, extra={'format': 'HTML'}) else: return self.bot.send_message( m, self.bot.trans.plugins.administration.strings. not_added % m.conversation.title, extra={'format': 'HTML'}) elif first_word(input) == 'rules': if gid in self.bot.administration: self.bot.administration[ gid].rules = all_but_first_word( all_but_first_word(input).split('\n')[0:]) set_data( 'administration/%s/%s/rules' % (self.bot.name, gid), self.bot.administration[gid].rules) return self.bot.send_message( m, self.bot.trans.plugins.administration.strings.set % m.conversation.title, extra={'format': 'HTML'}) else: return self.bot.send_message( m, self.bot.trans.plugins.administration.strings. not_added % m.conversation.title, extra={'format': 'HTML'}) elif first_word(input) == 'rule': if gid in self.bot.administration: try: i = int(first_word(all_but_first_word(input))) - 1 if i > len(self.bot.administration[gid].rules): i = len(self.bot.administration[gid].rules) elif i < 1: i = 0 except: return self.bot.send_message( m, self.bot.trans.errors.unknown, extra={'format': 'HTML'}) self.bot.administration[gid].rules.insert( i, all_but_first_word(all_but_first_word(input))) set_data( 'administration/%s/%s/rules' % (self.bot.name, gid), self.bot.administration[gid].rules) return self.bot.send_message( m, self.bot.trans.plugins.administration.strings.set % m.conversation.title, extra={'format': 'HTML'}) else: return self.bot.send_message( m, self.bot.trans.plugins.administration.strings. not_added % m.conversation.title, extra={'format': 'HTML'}) elif first_word(input) == 'public': if gid in self.bot.administration: if self.bot.trans.plugins.administration.strings.yes.lower( ) in all_but_first_word(input).lower(): self.bot.administration[gid].public = True else: self.bot.administration[gid].public = False set_data( 'administration/%s/%s/public' % (self.bot.name, gid), self.bot.administration[gid].public) return self.bot.send_message( m, self.bot.trans.plugins.administration.strings.set % m.conversation.title, extra={'format': 'HTML'}) else: return self.bot.send_message( m, self.bot.trans.plugins.administration.strings. not_added % m.conversation.title, extra={'format': 'HTML'}) else: return self.bot.send_message( m, self.bot.trans.errors.no_results, extra={'format': 'HTML'})
def run(self, m): input = get_input(m) # List all pins # if is_command(self, 1, m.content): pins = [] for pin in self.bot.pins: if 'creator' in self.bot.pins[pin] and self.bot.pins[ pin].creator == m.sender.id: pins.append(pin) if len(pins) > 0: text = self.bot.trans.plugins.pins.strings.pins % len(pins) for pin in pins: text += '\n • #%s' % pin else: text = self.bot.trans.plugins.pins.strings.no_pins # If the message is too long send an error message instead # if len(text) <= 4096: return self.bot.send_message(m, text, extra={'format': 'HTML'}) else: return self.bot.send_message(m, self.bot.trans.errors.unknown, extra={'format': 'HTML'}) # Add a pin # elif is_command(self, 2, m.content): if not input: return self.bot.send_message(m, generate_command_help( self, m.content), extra={'format': 'HTML'}) # return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'}) if input.startswith('#'): input = input.lstrip('#') input = input.lower() if not m.reply: return self.bot.send_message(m, self.bot.trans.errors.needs_reply, extra={'format': 'HTML'}) if input in self.bot.pins: return self.bot.send_message( m, self.bot.trans.plugins.pins.strings.already_pinned % input, extra={'format': 'HTML'}) if m.reply.type == 'text' and m.reply.content.startswith( self.bot.config.prefix): pin_type = 'command' else: pin_type = m.reply.type self.bot.pins[input] = { 'content': m.reply.content.replace('<', '<').replace('>', '>'), 'creator': m.sender.id, 'type': pin_type } set_data('pins/%s/%s' % (self.bot.name, input), self.bot.pins[input]) self.update_triggers() return self.bot.send_message( m, self.bot.trans.plugins.pins.strings.pinned % input, extra={'format': 'HTML'}) # Remove a pin # elif is_command(self, 3, m.content): if not input: return self.bot.send_message(m, generate_command_help( self, m.content), extra={'format': 'HTML'}) # return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'}) if input.startswith('#'): input = input.lstrip('#') input = input.lower() if not input in self.bot.pins: return self.bot.send_message( m, self.bot.trans.plugins.pins.strings.not_found % input, extra={'format': 'HTML'}) if not m.sender.id == self.bot.pins[input].creator: return self.bot.send_message( m, self.bot.trans.plugins.pins.strings.not_creator % input, extra={'format': 'HTML'}) delete_data('pins/%s/%s' % (self.bot.name, input)) del self.bot.pins[input] self.update_triggers() return self.bot.send_message( m, self.bot.trans.plugins.pins.strings.unpinned % input, extra={'format': 'HTML'}) # Check what pin was triggered # else: # Finds the first 3 pins of the message and sends them. # pins = findall(r"#(\w+)", m.content.lower()) count = 3 for pin in pins: if pin in self.bot.pins: # You can reply with a pin and the message will reply too. # if m.reply: reply = m.reply.id else: reply = m.id if 'content' in self.bot.pins[ pin] and 'type' in self.bot.pins[pin]: if (self.bot.pins[pin].type == 'command'): m.content = self.bot.pins[pin].content self.bot.inbox.put(m) return self.bot.on_message_receive(m) else: self.bot.send_message(m, self.bot.pins[pin].content, self.bot.pins[pin].type, extra={'format': 'HTML'}, reply=reply) count -= 1 if count == 0: return
def run(self, m): input = get_input(m) uid = str(m.sender.id) gid = str(m.conversation.id) # List all administration commands. # if is_command(self, 1, m.content): text = self.bot.trans.plugins.administration.strings.commands for command in self.commands: # Adds the command and parameters# if command.command == '^new_chat_member$': text += '\n • ' + \ self.bot.trans.plugins.administration.strings.new_chat_member else: text += '\n • ' + \ command.command.replace('/', self.bot.config.prefix) if 'parameters' in command and command.parameters: for parameter in command.parameters: name, required = list(parameter.items())[0] # Bold for required parameters, and italic for optional # if required: text += ' <b><%s></b>' % name else: text += ' [%s]' % name if 'description' in command: text += '\n <i>%s</i>' % command.description else: text += '\n <i>?¿</i>' return self.bot.send_message(m, text, extra={'format': 'HTML'}) # List all groups. # if is_command(self, 2, m.content): text = self.bot.trans.plugins.administration.strings.groups if len(self.bot.administration) > 0: for gid, attr in self.bot.administration.items(): if 'public' in self.bot.administration[gid] and self.bot.administration[gid].public: if 'link' in self.bot.administration[gid] and self.bot.config.bindings != 'telegram-cli': text += '\n • <a href="%s">%s</a>' % ( self.bot.administration[gid].link, self.bot.groups[gid].title) else: text += '\n • %s' % self.bot.groups[gid].title if self.bot.administration[gid].alias: text += ' /<b>%s</b>/' % attr.alias else: text = self.bot.trans.plugins.administration.strings.no_groups return self.bot.send_message(m, text, extra={'format': 'HTML', 'preview': False}) # Join a group. # elif is_command(self, 3, m.content): if not input: return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'}) gid_to_join = None for id in self.bot.administration: if (input in self.bot.administration or compile('^' + input.lower() + '$').search(self.bot.administration[id].alias.lower()) or compile('^' + input.lower() + '$').search(self.bot.groups[id].title.lower())): gid_to_join = id break if gid_to_join: if not gid_to_join in self.bot.administration: return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.not_added % m.conversation.title, extra={'format': 'HTML'}) added = self.bot.bindings.invite_conversation_member( gid_to_join, uid) if not added: text = '<b>%s</b>' % self.bot.groups[gid_to_join].title if self.bot.administration[gid_to_join].motd: text += '\n<i>%s</i>' % self.bot.administration[gid_to_join].motd text += '\n\n%s' % self.bot.trans.plugins.administration.strings.rules i = 1 if 'rules' in self.bot.administration[gid_to_join]: for rule in self.bot.administration[gid_to_join].rules: text += '\n %s. <i>%s</i>' % (i, rule) i += 1 else: text += '\n%s' % self.bot.trans.plugins.administration.strings.norules if not self.bot.administration[gid_to_join].link: text += '\n\n%s' % self.bot.trans.plugins.administration.strings.nolink else: text += '\n\n<a href="%s">%s</a>' % ( self.bot.administration[gid_to_join].link, self.bot.trans.plugins.administration.strings.join) return self.bot.send_message(m, text, extra={'format': 'HTML', 'preview': False}) # Information about a group. # elif is_command(self, 4, m.content) or is_command(self, 16, m.content): if m.conversation.id > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) if not gid in self.bot.administration: if is_command(self, 4, m.content): return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.not_added % m.conversation.title, extra={'format': 'HTML'}) elif is_command(self, 16, m.content): return text = '<b>%s</b>' % self.bot.groups[gid].title if 'motd' in self.bot.administration[gid]: text += '\n<i>%s</i>' % self.bot.administration[gid].motd text += '\n\n%s' % self.bot.trans.plugins.administration.strings.rules i = 1 if not 'rules' in self.bot.administration[gid]: text += '\n%s' % self.bot.trans.plugins.administration.strings.norules else: for rule in self.bot.administration[gid].rules: text += '\n %s. <i>%s</i>' % (i, rule) i += 1 if is_command(self, 4, m.content): if not self.bot.administration[gid].link: text += '\n\n%s' % self.bot.trans.plugins.administration.strings.nolink else: text += '\n\n<a href="%s">%s</a>' % ( self.bot.trans.plugins.administration.strings.join, self.bot.administration[gid].link) return self.bot.send_message(m, text, extra={'format': 'HTML', 'preview': False}) # Rules of a group. # elif is_command(self, 5, m.content): if m.conversation.id > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) if not gid in self.bot.administration: return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.not_added % m.conversation.title, extra={'format': 'HTML'}) if input and is_int(input): try: i = int(input) text = '%s. <i>%s</i>' % (i, self.bot.administration[gid].rules[i - 1]) except: text = self.bot.trans.plugins.administration.strings.notfound else: text = self.bot.trans.plugins.administration.strings.rules i = 1 for rule in self.bot.administration[gid].rules: text += '\n %s. <i>%s</i>' % (i, rule) i += 1 if not self.bot.administration[gid].rules: text += '\n%s' % self.bot.trans.plugins.administration.strings.norules return self.bot.send_message(m, text, extra={'format': 'HTML', 'preview': False}) # Set rules # elif is_command(self, 6, m.content): if not input: return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'}) if m.conversation.id > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) if not is_admin(self.bot, uid, m) and not is_mod(self.bot, uid, gid): return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'}) if gid in self.bot.administration: self.bot.administration[gid].rules = input.split('\n') set_data('administration/%s/%s/rules' % (self.bot.name, gid), self.bot.administration[gid].rules) return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.set % m.conversation.title, extra={'format': 'HTML'}) else: return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.not_added % m.conversation.title, extra={'format': 'HTML'}) # Set rule # elif is_command(self, 7, m.content): if not input: return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'}) if m.conversation.id > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) if not is_admin(self.bot, uid, m) and not is_mod(self.bot, uid, gid): return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'}) if gid in self.bot.administration: try: i = int(first_word(input))-1 if i > len(self.bot.administration[gid].rules): i = len(self.bot.administration[gid].rules) elif i < 1: i = 0 except: return self.bot.send_message(m, self.bot.trans.errors.invalid_syntax, extra={'format': 'HTML'}) self.bot.administration[gid].rules.insert( i, all_but_first_word(input)) set_data('administration/%s/%s/rules' % (self.bot.name, gid), self.bot.administration[gid].rules) return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.set % m.conversation.title, extra={'format': 'HTML'}) # Remove rule # elif is_command(self, 8, m.content): if not input: return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'}) if m.conversation.id > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) if not is_admin(self.bot, uid, m) and not is_mod(self.bot, uid, gid): return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'}) if gid in self.bot.administration: try: i = int(first_word(input))-1 if i > len(self.bot.administration[gid].rules): i = len(self.bot.administration[gid].rules) elif i < 1: i = 0 except: return self.bot.send_message(m, self.bot.trans.errors.invalid_syntax, extra={'format': 'HTML'}) del self.bot.administration[gid].rules[i] set_data('administration/%s/%s/rules' % (self.bot.name, gid), self.bot.administration[gid].rules) return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.set % m.conversation.title, extra={'format': 'HTML'}) # Add mod # elif is_command(self, 9, m.content): if m.conversation.id > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) if not is_admin(self.bot, uid, m): return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'}) set_tag(self.bot, m.sender.id, 'mod:%s' % gid) # Remove mod # elif is_command(self, 10, m.content): if m.conversation.id > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) if not is_admin(self.bot, uid, gid): return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'}) del_tag(self.bot, m.sender.id, 'mod:%s' % gid) # Add group # elif is_command(self, 11, m.content): if m.conversation.id > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) if not is_admin(self.bot, uid, m): return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'}) if not gid in self.bot.administration: self.bot.administration[gid] = { 'alias': None, 'description': None, 'link': None, 'rules': [], 'public': False } set_data('administration/%s/%s' % (self.bot.name, gid), self.bot.administration[gid]) return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.added % m.conversation.title, extra={'format': 'HTML'}) else: return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.already_added % m.conversation.title, extra={'format': 'HTML'}) # Remove group # elif is_command(self, 12, m.content): if m.conversation.id > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) if not is_admin(self.bot, uid, m): return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'}) if gid in self.bot.administration: del self.bot.administration[gid] delete_data('administration/%s/%s' % (self.bot.name, gid)) return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.removed % m.conversation.title, extra={'format': 'HTML'}) else: return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.not_added % m.conversation.title, extra={'format': 'HTML'}) # Set alias # elif is_command(self, 13, m.content): if m.conversation.id > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) if not is_admin(self.bot, uid, m): return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'}) if gid in self.bot.administration: self.bot.administration[gid].alias = input.lower() set_data('administration/%s/%s/alias' % (self.bot.name, gid), self.bot.administration[gid].alias) return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.set % m.conversation.title, extra={'format': 'HTML'}) else: return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.not_added % m.conversation.title, extra={'format': 'HTML'}) # Set link # elif is_command(self, 14, m.content): if m.conversation.id > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) if not is_admin(self.bot, uid, m): return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'}) if gid in self.bot.administration: self.bot.administration[gid].link = input.lower() set_data('administration/%s/%s/link' % (self.bot.name, gid), self.bot.administration[gid].link) return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.set % m.conversation.title, extra={'format': 'HTML'}) else: return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.not_added % m.conversation.title, extra={'format': 'HTML'}) # Make public # elif is_command(self, 15, m.content): if not input: return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'}) if gid in self.bot.administration: if input and 'true' in input.lower(): self.bot.administration[gid].public = True else: self.bot.administration[gid].public = False set_data('administration/%s/%s/public' % (self.bot.name, gid), self.bot.administration[gid].public) return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.set % m.conversation.title, extra={'format': 'HTML'}) else: return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.not_added % m.conversation.title, extra={'format': 'HTML'})