def add_contact(message): """Add a new Whatsapp contact to the database. Message has the following format: /add <name> <phone> Args: message: Received Telegram message. """ if message.chat.id != SETTINGS['owner']: tgbot.reply_to(message, 'You are not the owner of this bot') return # Get name and phone args = telebot.util.extract_arguments(message.text) name, phone = args.split(maxsplit=1) if not name or not phone: tgbot.reply_to(message, 'Syntax: /add <name> <phone>') return # Check if it already exists if get_contact(phone) or get_phone(name): tgbot.reply_to(message, 'A contact with those details already exists') return # Add to database db_add_contact(name, phone) tgbot.reply_to(message, 'Contact added')
def rm_contact(message): """Remove a Whatsapp contact from the database. Message has the following format: /rm <name> Args: message: Received Telegram message. """ if message.chat.id != SETTINGS['owner']: tgbot.reply_to(message, 'you are not the owner of this bot') return # Get name name = telebot.util.extract_arguments(message.text) if not name: tgbot.reply_to(message, 'Syntax: /rm <name>') return # Check if it already exists if not get_phone(name): tgbot.reply_to(message, 'No contact found with that name') return # Add to database db_rm_contact(name) tgbot.reply_to(message, 'Contact removed')
def to_wa_handler(sender, **kwargs): """Handle signals sent to Whatsapp. This will involve sending messages through the Whatsapp bot. Args: contact (str): Name of the contact to send the message to. message (str): The message to send """ contact = kwargs.get('contact') message = kwargs.get('message') # Check if known contact phone = get_phone(contact) if not phone: # Abort tgbot.send_message(SETTINGS['owner'], 'Unknown contact: "%s"' % contact) return logger.info('sending message to %s (%s)' % (contact, phone)) wabot.send_msg(phone=phone, message=message)
def bind(message): """Bind a contact to a group. Message has the following format: /bind <name> <group id> Args: message: Received Telegram message. """ if message.chat.id != SETTINGS['owner']: tgbot.reply_to(message, 'You are not the owner of this bot') return # Get name and phone args = telebot.util.extract_arguments(message.text) name, group_id = args.split(maxsplit=1) if not name or not group_id: tgbot.reply_to(message, 'Syntax: /bind <name> <group id>') return group_id = safe_cast(group_id, int) if not group_id: tgbot.reply_to(message, 'Group id has to be a number') return # Ensure contact exists if not get_phone(name): tgbot.reply_to(message, 'No contact found with that name') return # Check if it already exists current = db_get_contact_by_group(group_id) if current: tgbot.reply_to(message, 'This group is already bound to ' + current) return # Add to database db_set_group(name, group_id) tgbot.reply_to(message, 'Bound to group')
def unbind(message): """Unbind a contact from his group. Message has the following format: /unbind <name> Args: message: Received Telegram message. """ if message.chat.id != SETTINGS['owner']: tgbot.reply_to(message, 'You are not the owner of this bot') return # Get name and phone name = telebot.util.extract_arguments(message.text) if not name: tgbot.reply_to(message, 'Syntax: /unbind <name>') return # Ensure contact exists if not get_phone(name): tgbot.reply_to(message, 'No contact found with that name') return # Check if it already exists group = db_get_group(name) if not group: tgbot.reply_to(message, 'Contact was not bound to a group') return # Add to database db_set_group(name, None) tgbot.reply_to(message, 'Unbound from group')