def cmd_sub_no_reply(update: telegram.Update, context: CallbackContext) -> None: args = context.args bot = context.bot chat, _created = TelegramChat.get_or_create( chat_id=update.message.chat.id, tg_type=update.message.chat.type, ) if len(args) < 1: bot.reply(update, "Use /sub_no_reply username1 username2 username3 ...") return tw_usernames = args not_found = [] already_subscribed = [] successfully_subscribed = [] for tw_username in tw_usernames: tw_user = bot.get_tw_user(tw_username) if tw_user is None: not_found.append(tw_username) continue if Subscription.select().where( Subscription.tw_user == tw_user, Subscription.tg_chat == chat).count() == 1: already_subscribed.append(tw_user.full_name) continue Subscription.create(tg_chat=chat, tw_user=tw_user, sub_kind=2) successfully_subscribed.append(tw_user.full_name) reply = "" if len(not_found) != 0: reply += "Sorry, I didn't find username{} {}\n\n".format( "" if len(not_found) == 1 else "s", ", ".join(not_found)) if len(already_subscribed) != 0: reply += "You're already subscribed to {}\n\n".format( ", ".join(already_subscribed)) if len(successfully_subscribed) != 0: reply += "I've added your subscription to {}".format( ", ".join(successfully_subscribed)) bot.reply(update, reply)
def subscribe(subscriber, resource): # HACK workaround for using get_or_create / create_or_get with GFKFields. try: # HACK workaround for querying equality of GFKField. I'd love to do the following: # Subscription.get( # (Subscription.subscriber == subscriber) & # (Subscription.resource == resource)) sub = Subscription.get( (Subscription.subscriber_id == subscriber._get_pk_value()) & (Subscription.subscriber_type == subscriber._meta.db_table) & (Subscription.resource_id == resource._get_pk_value()) & (Subscription.resource_type == resource._meta.db_table)) created = False except Subscription.DoesNotExist: sub = Subscription.create( subscriber=subscriber, resource=resource, ) created = True return sub, created
def cmd_subChan(bot, update, args, chat=None): logging.getLogger("subChan").debug("subChan for {}".format(args)) if len(args) < 1: bot.reply( update, "Use /sub username1 chatid , get it from https://api.telegram.org/bot<botid>/getUpdates after doing a post in the channel (public or private) with the robot disabled" ) return tw_username = args[0] chatId = args[1] newchat, _created = TelegramChat.get_or_create(chat_id=chatId, tg_type="channel") not_found = [] already_subscribed = [] successfully_subscribed = [] chat.chat_id = chatId tw_user = bot.get_tw_user(tw_username) if tw_user is None: not_found.append(tw_username) return if Subscription.select().where( Subscription.tw_user == tw_user, Subscription.tg_chat == newchat).count() == 1: already_subscribed.append(tw_user.full_name) else: Subscription.create(tg_chat=newchat, tw_user=tw_user) successfully_subscribed.append(tw_user.full_name) reply = "" if len(not_found) is not 0: reply += "Sorry, I didn't find username{} {}\n\n".format( "" if len(not_found) is 1 else "s", ", ".join(not_found)) if len(already_subscribed) is not 0: reply += "You're already subscribed to {}\n\n".format( ", ".join(already_subscribed)) if len(successfully_subscribed) is not 0: reply += "I've added your subscription to {} with chat {} ".format( ", ".join(successfully_subscribed), chat.chat_id) bot.reply(update, reply)
def cmd_sub(self, msg, args, chat=None): if len(args) < 1: self.reply(msg, "Use /sub username1 username2 username3 ...") return tw_usernames = args not_found = [] already_subscribed = [] successfully_subscribed = [] for tw_username in tw_usernames: tw_user = self.get_tw_user(tw_username) if tw_user is None: not_found.append(tw_username) continue if Subscription.select().where( Subscription.tw_user == tw_user, Subscription.tg_chat == chat).count() == 1: already_subscribed.append(tw_user.full_name) continue Subscription.create(tg_chat=chat, tw_user=tw_user) successfully_subscribed.append(tw_user.full_name) reply = "" if len(not_found) is not 0: reply += "Sorry, I didn't find username{} {}\n\n".format( "" if len(not_found) is 1 else "s", ", ".join(not_found) ) if len(already_subscribed) is not 0: reply += "You're already subscribed to {}\n\n".format( ", ".join(already_subscribed) ) if len(successfully_subscribed) is not 0: reply += "I've added your subscription to {}".format( ", ".join(successfully_subscribed) ) self.reply(msg, reply)
def add_feed_to_group(self, user, feed, group): s = Subscription.create(user=user, feed=feed, group=group) return s
def add_feed_to_group(self, user, feed, group): s = Subscription.create(user = user, feed = feed, group = group) db.close() return s