Пример #1
0
def setting(update, content):
    try:
        if update.callback_query:
            data = update.callback_query.data
            admin, name = data.split(';')
            if not admin == '_':
                channel = db.find("channel", admin=admin, name=name)
            else:
                channel = db.find('channel', name=name)
            chat_id = update.callback_query.message.chat_id
            message_id = update.callback_query.message.message_id
            text, keyboard = strings.status(
                channel=channel, remain=util.remain(channel=channel))
            content.bot.edit_message_text(chat_id=chat_id,
                                          text=text,
                                          message_id=message_id,
                                          reply_markup=keyboard)
            return select
        else:
            um = update.message
            admin = um.from_user
            channel = db.find('channel', admin=admin)
            text, keyboard = strings.status(
                channel=channel, remain=util.remain(channel=channel))
            content.bot.send_message(chat_id=admin.id,
                                     text=text,
                                     reply_markup=keyboard)
            return select
    except Exception as E:
        logging.error("setting {}".format(E))
Пример #2
0
def status(update, content):
    try:
        um = update.message
        admin = um.from_user.id
        channels = db.find('channel', admin=admin)
        if channels:
            if isinstance(channels, db.Channel):
                channel = channels
                chat_id = um.chat_id
                message_id = um.message_id

                text, keyboard = strings.status(
                    channel=channel, remain=util.remain(channel=channel))
                content.bot.send_message(chat_id=chat_id,
                                         text=text,
                                         reply_to_message_id=message_id,
                                         reply_markup=keyboard)
                return select

            elif isinstance(channels, list):
                keyboard = InlineKeyboardMarkup(
                    [[Inline(i.name, callback_data="_;{}".format(i.name))]
                     for i in channels])
                content.bot.send_message(
                    chat_id=admin,
                    text=
                    "شما صاحب چندین کانال هستید\nمایل به مشاهده تنظیمات کدام یک هستید؟",
                    reply_markup=keyboard)
                return setting

    except Exception as E:
        logging.error("status {}".format(E))
Пример #3
0
    def state(self, update, content):
        try:
            admin = update.message.chat_id

            if not content.args:
                channel = db.find("channel", admin=admin)
            else:
                name = content.args[0]
                channel = db.find("channel", admin=admin, name=name)

            if isinstance(channel, db.Channel):
                text = strings.status(channel,
                                      util.remain(channel=channel),
                                      button=False)
                self.updater.bot.send_message(
                    chat_id=admin,
                    text=text,
                    reply_to_message_id=update.message.message_id)
                logging.info("state : {}".format(channel.name))

            elif isinstance(channel, list):
                self.updater.bot.send_message(
                    chat_id=admin,
                    text=
                    "شما صاحب چندین نسخه از بات هستید لطفا نام کانال خود را نیز وارد کنید"
                    "مثال:\n"
                    "/state @channel",
                    reply_to_message_id=update.message.message_id)

        except Exception as E:
            logging.error("state : {}".format(E))
Пример #4
0
def done(update, content):
    try:
        if update.callback_query:
            um = update.callback_query
            data = um.data
            part1, channel_name = data.split(';')
            chat_id = um.message.chat_id
            message_id = um.message.message_id

            channel = db.find('channel', name=channel_name)

            if part1 == '_':
                pass
            elif part1.endswith('b'):
                bed = part1[:-1]
                channel.bed = int(bed)
                db.update(channel)
            elif part1.endswith('w'):
                wake = part1[:-1]
                channel.wake = int(wake)
                db.update(channel)
            elif part1.endswith("r") or part1.endswith("f"):
                interval = part1
                channel.interval = interval
                db.update(channel)
            elif part1 == "off":
                channel.bed = -1
                channel.wake = -1
                db.update(channel)

            text, keyboard = strings.status(
                channel=channel, remain=util.remain(channel=channel))
            content.bot.edit_message_text(chat_id=chat_id,
                                          message_id=message_id,
                                          text=text,
                                          reply_markup=keyboard)
            return select

    except Exception as E:
        logging.error("done {}".format(E))
Пример #5
0
def step2(update, content):
    if update.callback_query:
        um = update.callback_query
        data = um.data.split(';')
        interval = data[0]
        ch_name = data[1]
        admin = um.message.chat_id
        message_id = um.message.message_id

        if interval.endswith('m'):
            interval = int(interval[:-1])
            if 1 <= interval <= 30:
                content.bot.edit_message_text(
                    chat_id=um.message.chat_id,
                    message_id=um.message.message_id,
                    text="گام دوم:\n"
                    "1️⃣ پیام ها هر {0} دقیقه ارسال شوند. برای مثال بصورت 01:{0}, "
                    "01:{1} و ...\n"
                    "2️⃣ پیام ها وقتی دقیقه شمار برابر {0} است ارسال شوند برای مثال "
                    "01:{0}, 02:{0}, 03:{0} و ...\n"
                    "کدام یک مورد پسند شماست؟\n"
                    "از ما کمک بگیرید:\n"
                    "@s_for_cna".format(
                        str(interval).zfill(2),
                        str(interval * 2).zfill(2)),
                    reply_markup=InlineKeyboardMarkup(
                        [[
                            Inline('حالت  1️⃣',
                                   callback_data='{}mr;{}'.format(
                                       interval, ch_name)),
                            Inline('حالت  2️⃣',
                                   callback_data='{}mf;{}'.format(
                                       interval, ch_name))
                        ],
                         [
                             Inline('لغو، بازگشت',
                                    callback_data='_;{}'.format(ch_name))
                         ]]))
            else:
                content.bot.edit_message_text(
                    chat_id=um.message.chat_id,
                    message_id=um.message.message_id,
                    text="گام دوم:\n"
                    "پیام ها در ساعاتی مانند 01:{0}, 02:{0} ارسال میشوند\n"
                    "از ما کمک بگیرید:\n"
                    "@s_for_cna".format(str(interval).zfill(2)),
                    reply_markup=InlineKeyboardMarkup(
                        [[
                            Inline('تایید',
                                   callback_data='{}mr;{}'.format(
                                       interval, ch_name))
                        ],
                         [
                             Inline('لغو، بازگشت',
                                    callback_data='_;{}'.format(ch_name))
                         ]]))

            return done
        elif interval.endswith('h'):
            interval = int(interval[:-1])
            if 1 <= interval <= 12:
                content.bot.edit_message_text(
                    chat_id=um.message.chat_id,
                    message_id=um.message.message_id,
                    text="گام دوم:\n"
                    "1️⃣ پیام ها هر {0} ساعت ارسال شوند. برای مثال بصورت {0}:00, "
                    " {1}:00 و ... "
                    "2️⃣ پیام ها هر روز راس ساعت {0}:00 ارسال شوند"
                    "کدام یک مورد پسند شماست؟\n"
                    "از ما کمک بگیرید:\n"
                    "@s_for_cna".format(
                        str(interval).zfill(2),
                        str(interval * 2).zfill(2)),
                    reply_markup=InlineKeyboardMarkup(
                        [[
                            Inline('حالت  1️⃣',
                                   callback_data='{}hr;{}'.format(
                                       interval, ch_name)),
                            Inline('حالت  2️⃣',
                                   callback_data='{}hf;{}'.format(
                                       interval, ch_name))
                        ],
                         [
                             Inline('لغو، بازگشت',
                                    callback_data='_;{}'.format(ch_name))
                         ]]))
            else:
                content.bot.edit_message_text(
                    chat_id=um.message.chat_id,
                    message_id=um.message.message_id,
                    text="گام دوم:\n"
                    "پیام ها هر روز راس ساعت {0}:00 ارسال شوند"
                    "کدام یک مورد پسند شماست؟\n"
                    "از ما کمک بگیرید:\n"
                    "@s_for_cna".format(str(interval).zfill(2)),
                    reply_markup=InlineKeyboardMarkup(
                        [[
                            Inline('تایید',
                                   callback_data='{}mr;{}'.format(
                                       interval, ch_name))
                        ],
                         [
                             Inline('لغو، بازگشت',
                                    callback_data='_;{}'.format(ch_name))
                         ]]))
            return done
        elif interval == "setting":
            channel = db.find("channel", name=ch_name)
            text, keyboard = strings.status(
                channel=channel, remain=util.remain(channel=channel))
            content.bot.edit_message_text(chat_id=admin,
                                          text=text,
                                          message_id=message_id,
                                          reply_markup=keyboard)
            return select
Пример #6
0
    def admin(self, update, content):
        try:
            chat_id = update.message.chat_id
            message_id = update.message.message_id
            command = group_id = admin = channel_name = plan = expire = None
            if content.args:
                self.updater.bot.send_chat_action(
                    chat_id=chat_id, action=telegram.ChatAction.TYPING)
                command = content.args[0]
                if command == "add":
                    group_id, admin, channel_name, plan, expire = content.args[
                        1:]
                    if not db.find('channel', name=channel_name):
                        channel = db.Channel(
                            name=channel_name,
                            admin=int(admin),
                            group_id=int(group_id),
                            plan=int(plan),
                            expire=timedelta(days=int(expire)))
                        db.add(channel)
                        self.updater.bot.send_message(
                            chat_id=chat_id,
                            reply_to_message_id=message_id,
                            text="ثبت شد \n\n{}".format(channel.__str__()))
                elif command == "ren":
                    channel_name, expire = content.args[1:]
                    if db.find("channel", name=channel_name):
                        channel = db.find("channel", name=channel_name)
                        channel.expire += timedelta(days=int(expire))
                        db.update(channel)
                        self.updater.bot.send_message(
                            chat_id=chat_id,
                            reply_to_message_id=message_id,
                            text="ثبت شد \n\n{}".format(channel.__str__()))
                elif command == "plan":
                    channel_name, plan = content.args[1:]
                    channel = db.find("channel", name=channel_name)
                    channel.plan = int(plan)
                    db.update(channel)
                    self.updater.bot.send_message(
                        chat_id=chat_id,
                        reply_to_message_id=message_id,
                        text="ثبت شد \n\n{}".format(channel.__str__()))
                elif command == "del":
                    channel_name = content.args[1]
                    channel = db.find("channel", name=channel_name)
                    if channel:
                        db.delete(channel)
                elif command == "edit":
                    channel_name, n_channel_name = content.args[1:]
                    channel = db.find("channel", name=channel_name)
                    if channel:
                        channel.name = n_channel_name
                        db.update(channel)
                        self.updater.bot.send_message(
                            chat_id=chat_id,
                            reply_to_message_id=message_id,
                            text="ثبت شد \n\n{}".format(channel.__str__()))
                elif command == "lst":
                    channels = db.find('channel')
                    text = "channel          expire_date\n"
                    for ch in channels:
                        expire = JalaliDatetime().from_date(ch.expire)
                        now = JalaliDatetime().now()
                        diff = expire - now
                        if diff.days < 7:
                            text += "{} {} 🔴\n\n".format(
                                ch.name, expire.strftime("%A %d %B"))
                        else:
                            text += "{} {} ⚪️\n\n".format(
                                ch.name, expire.strftime("%A %d %B"))

                    self.updater.bot.send_message(chat_id=cna, text=text)
                elif command == "det":
                    channel_name, = content.args[1:]
                    channel = db.find("channel", name=channel_name)
                    if isinstance(channel, db.Channel):
                        self.updater.bot.send_message(
                            chat_id=chat_id,
                            reply_to_message_id=message_id,
                            text=strings.status(channel,
                                                util.remain(channel),
                                                button=False))
                elif command == "db":
                    # db
                    self.updater.bot.send_document(chat_id=cna,
                                                   document=open(
                                                       "bot_db.db", "rb"),
                                                   timeout=time_out)
                else:
                    self.updater.bot.send_message(
                        chat_id=chat_id,
                        reply_to_message_id=message_id,
                        text="command {} not found".format(content.args[0]))

            else:
                self.updater.bot.send_message(chat_id=chat_id,
                                              text=strings.admin_hint)
        except Exception as E:
            logging.error("admin: {}".format(E))