def verify_button_pressed(update, context):
	chat = update.effective_chat  # type: Optional[Chat]
	user = update.effective_user  # type: Optional[User]
	query = update.callback_query  # type: Optional[CallbackQuery]
	match = re.match(r"verify_me\((.+?)\)", query.data)
	match = match.group(1).split("|")
	is_ok = match[0]
	user_id = match[1]
	chat_id = match[2]
	message = update.effective_message  # type: Optional[Message]
	print("-> {} was clicked welcome verify button".format(user.id))
	if is_ok == "y":
		if context.bot.getChatMember(chat_id, user_id).status in ('left'):
			query.answer(text=tl(update.effective_message, "Failed: user left chat"))
			return
		try:
			context.bot.restrict_chat_member(chat_id, user_id, permissions=ChatPermissions(can_send_messages=True, can_send_media_messages=True, can_send_polls=True, can_send_other_messages=True, can_add_web_page_previews=True, can_invite_users=True))
			sql.add_to_userlist(chat_id, user_id, True)
			sql.rm_from_timeout(chat_id, user_id)
		except BadRequest as err:
			if not update.effective_chat.get_member(context.bot.id).can_restrict_members:
				query.answer(text=tl(update.effective_message, "Saya tidak dapat membatasi orang disini, tanya admin untuk unmute!"))
			else:
				query.answer(text="Error: " + str(err))
			return
		chat_name = context.bot.get_chat(chat_id).title
		context.bot.edit_message_media(chat.id, message_id=query.message.message_id, media=InputMediaPhoto(media="https://telegra.ph/file/06d2c5ec80af3858c2d4b.jpg", caption=tl(update.effective_message, "*Berhasil!*\n\nKerja bagus manusia, kini Anda dapat chatting di: *{}*").format(chat_name), parse_mode="markdown"))
		query.answer(text=tl(update.effective_message, "Berhasil! Anda dapat chatting di {} sekarang").format(chat_name), show_alert=True)
	else:
		context.bot.edit_message_media(chat.id, message_id=query.message.message_id, media=InputMediaPhoto(media="https://telegra.ph/file/d81cdcbafb240071add84.jpg", caption=tl(update.effective_message, "Maaf robot, kamu telah salah klik tombol verifikasi.\n\nCoba lagi dengan klik tombol verifikasi pada pesan selamat datang."), parse_mode="markdown"))
		query.answer(text=tl(update.effective_message, "Gagal! Kamu telah salah mengklik tombol verifikasi"), show_alert=True)
Beispiel #2
0
def verify_button_pressed(update, context):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    query = update.callback_query  # type: Optional[CallbackQuery]
    match = re.match(r"verify_me\((.+?)\)", query.data)
    match = match.group(1).split("|")
    is_ok = match[0]
    user_id = match[1]
    chat_id = match[2]
    message = update.effective_message  # type: Optional[Message]
    print("-> {} was clicked welcome verify button".format(user.id))
    if is_ok == "y":
        if context.bot.getChatMember(chat_id, user_id).status in ('left'):
            query.answer(
                text=tl(update.effective_message, "Failed: user left chat"))
            return
        try:
            context.bot.restrict_chat_member(
                chat_id,
                user_id,
                permissions=ChatPermissions(can_send_messages=True,
                                            can_send_media_messages=True,
                                            can_send_polls=True,
                                            can_send_other_messages=True,
                                            can_add_web_page_previews=True,
                                            can_change_info=True,
                                            can_invite_users=True,
                                            can_pin_messages=True))
            sql.add_to_userlist(chat_id, user_id, True)
            sql.rm_from_timeout(chat_id, user_id)
        except BadRequest as err:
            if not update.effective_chat.get_member(
                    context.bot.id).can_restrict_members:
                query.answer(text=tl(
                    update.effective_message,
                    "I can't limit people here, asked the admin to unmute!"))
            else:
                query.answer(text="Error: " + str(err))
            return
        chat_name = context.bot.get_chat(chat_id).title
        # query.message.delete()
        # context.bot.send_photo(chat.id, photo="https://telegra.ph/file/06d2c5ec80af3858c2d4b.jpg", caption=tl(update.effective_message, "*It works!*\n\nGreat job man, now you can chat on: *{}*").format(chat_name), parse_mode="markdown")
        context.bot.edit_message_media(
            chat.id,
            message_id=query.message.message_id,
            media=InputMediaPhoto(
                media=open("emilia/modules/helper_funcs/emojis/done.jpg",
                           'rb'),
                caption=tl(
                    update.effective_message,
                    "*It works!*\n\nGreat job man, now you can chat on: *{}*").
                format(chat_name),
                parse_mode="markdown"))
        query.answer(
            text=tl(update.effective_message,
                    "It works! You can chat on {} now").format(chat_name),
            show_alert=True)
    elif is_ok == "re":
        user_id = update.effective_user.id
        is_clicked = sql.get_chat_userlist(chat_id)
        if user_id not in list(is_clicked):
            context.bot.edit_message_text(
                chat.id,
                message_id=query.message.message_id,
                text=tl(
                    update.effective_message,
                    "You are not in verification mode, if you are muted, you can ask the admin in the group concerned for help"
                ))
            return query.answer(text=tl(update.effective_message, "Error"),
                                show_alert=False)
        elif user_id in list(is_clicked) and is_clicked[user_id] == True:
            context.bot.edit_message_text(
                chat.id,
                message_id=query.message.message_id,
                text=tl(
                    update.effective_message,
                    "You are not in verification mode, if you are muted, you can ask the admin in the group concerned for help"
                ))
            return query.answer(text=tl(update.effective_message, "Error"),
                                show_alert=False)
        verify_code = [
            "🙏", "👈", "👉", "👇", "👆", "❤️", "🅰️", "🅱️", "0️⃣", "1️⃣", "2️⃣",
            "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣", "🔟"
        ]
        real_btn = random.choice(verify_code)
        print(real_btn)
        verify_code.remove(real_btn)
        verbox = (random.randint(1, 3), random.randint(1, 3))
        buttons = []
        linebox = []
        was_set = False
        for x in range(3):
            x += 1
            for y in range(3):
                y += 1
                if verbox[0] == y and verbox[1] == x:
                    was_set = True
                    print("button was set to " + real_btn + "at " +
                          str(verbox[0]) + " - " + str(verbox[1]))
                    linebox.append(
                        InlineKeyboardButton(
                            text=real_btn,
                            callback_data="verify_me(y|{}|{})".format(
                                user_id, chat_id)))
                else:
                    verify_emoji = random.choice(verify_code)
                    linebox.append(
                        InlineKeyboardButton(
                            text=verify_emoji,
                            callback_data="verify_me(n|{}|{})".format(
                                user_id, chat_id)))
                    verify_code.remove(verify_emoji)
            buttons.append(linebox)
            linebox = []
        if not was_set:
            verbox = (random.randint(1, 3), random.randint(1, 3))
            linebox[verbox[0] - 1][verbox[1] - 1] = InlineKeyboardButton(
                text=real_btn,
                callback_data="verify_me(y|{}|{})".format(user_id, chat_id))
            print("[x] button was set to " + real_btn + "at " +
                  str(verbox[0]) + " - " + str(verbox[1]))
        buttons.append([
            InlineKeyboardButton(text="Refresh",
                                 callback_data="verify_me(re|{}|{})".format(
                                     user_id, chat_id))
        ])
        # query.message.delete()
        # context.bot.send_photo(chat.id, photo=open("emilia/modules/helper_funcs/emojis/" + verify_code_images[real_btn], 'rb'), caption=tl(update.effective_message, "Please choose the same emoji below:") + "\n" + tl(update.effective_message, "If none of the emojis are the same, please click the refresh button"), parse_mode="markdown", reply_markup=InlineKeyboardMarkup(buttons))
        context.bot.edit_message_media(
            chat.id,
            message_id=query.message.message_id,
            media=InputMediaPhoto(
                media=open(
                    "emilia/modules/helper_funcs/emojis/" +
                    verify_code_images[real_btn], 'rb'),
                caption=tl(update.effective_message,
                           "Please choose the same emoji below:") + "\n" +
                tl(
                    update.effective_message,
                    "If none of the emojis are the same, please click the refresh button"
                ),
                parse_mode="markdown"),
            reply_markup=InlineKeyboardMarkup(buttons))
        query.answer(text=tl(update.effective_message, "Done"),
                     show_alert=False)
    else:
        # query.message.delete()
        # context.bot.send_photo(chat.id, photo=open("emilia/modules/helper_funcs/emojis/fail.jpg", 'rb'), caption=tl(update.effective_message, "Sorry robot, you have clicked the wrong verification button.\n\nTry again by clicking the verify button in the welcome message."), parse_mode="markdown")
        context.bot.edit_message_media(
            chat.id,
            message_id=query.message.message_id,
            media=InputMediaPhoto(
                media=open("emilia/modules/helper_funcs/emojis/fail.jpg",
                           'rb'),
                caption=tl(
                    update.effective_message,
                    "Sorry robot, you have clicked the wrong verification button..\n\nTry again by clicking the verify button in the welcome message."
                ),
                parse_mode="markdown"))
        query.answer(text=tl(
            update.effective_message,
            "Failed! You have clicked the verification button incorrectly"),
                     show_alert=True)
Beispiel #3
0
def verify_button_pressed(update, context):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    query = update.callback_query  # type: Optional[CallbackQuery]
    match = re.match(r"verify_me\((.+?)\)", query.data)
    match = match.group(1).split("|")
    is_ok = match[0]
    user_id = match[1]
    chat_id = match[2]
    message = update.effective_message  # type: Optional[Message]
    print("-> {} was clicked welcome verify button".format(user.id))
    if is_ok == "y":
        if context.bot.getChatMember(chat_id, user_id).status in ('left'):
            query.answer(
                text=tl(update.effective_message, "Failed: user left chat"))
            return
        try:
            context.bot.restrict_chat_member(
                chat_id,
                user_id,
                permissions=ChatPermissions(can_send_messages=True,
                                            can_send_media_messages=True,
                                            can_send_polls=True,
                                            can_send_other_messages=True,
                                            can_add_web_page_previews=True,
                                            can_change_info=True,
                                            can_invite_users=True,
                                            can_pin_messages=True))
            sql.add_to_userlist(chat_id, user_id, True)
            sql.rm_from_timeout(chat_id, user_id)
        except BadRequest as err:
            if not update.effective_chat.get_member(
                    context.bot.id).can_restrict_members:
                query.answer(text=tl(
                    update.effective_message,
                    "Saya tidak dapat membatasi orang disini, tanya admin untuk unmute!"
                ))
            else:
                query.answer(text="Error: " + str(err))
            return
        chat_name = context.bot.get_chat(chat_id).title
        # query.message.delete()
        # context.bot.send_photo(chat.id, photo="https://telegra.ph/file/06d2c5ec80af3858c2d4b.jpg", caption=tl(update.effective_message, "*Berhasil!*\n\nKerja bagus manusia, kini Anda dapat chatting di: *{}*").format(chat_name), parse_mode="markdown")
        context.bot.edit_message_media(
            chat.id,
            message_id=query.message.message_id,
            media=InputMediaPhoto(
                media=open("emilia/modules/helper_funcs/emojis/done.jpg",
                           'rb'),
                caption=tl(
                    update.effective_message,
                    "*Berhasil!*\n\nKerja bagus manusia, kini Anda dapat chatting di: *{}*"
                ).format(chat_name),
                parse_mode="markdown"))
        query.answer(text=tl(
            update.effective_message,
            "Berhasil! Anda dapat chatting di {} sekarang").format(chat_name),
                     show_alert=True)
    elif is_ok == "re":
        user_id = update.effective_user.id
        is_clicked = sql.get_chat_userlist(chat_id)
        if user_id not in list(is_clicked):
            context.bot.edit_message_text(
                chat.id,
                message_id=query.message.message_id,
                text=tl(
                    update.effective_message,
                    "Anda sedang tidak dalam mode verifikasi, jika anda sedang di bisukan, anda dapat meminta tolong pada admin di grup yang bersangkutan"
                ))
            return query.answer(text=tl(update.effective_message, "Error"),
                                show_alert=False)
        elif user_id in list(is_clicked) and is_clicked[user_id] == True:
            context.bot.edit_message_text(
                chat.id,
                message_id=query.message.message_id,
                text=tl(
                    update.effective_message,
                    "Anda sedang tidak dalam mode verifikasi, jika anda sedang di bisukan, anda dapat meminta tolong pada admin di grup yang bersangkutan"
                ))
            return query.answer(text=tl(update.effective_message, "Error"),
                                show_alert=False)
        verify_code = [
            "🙏", "👈", "👉", "👇", "👆", "❤️", "🅰️", "🅱️", "0️⃣", "1️⃣", "2️⃣",
            "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣", "🔟"
        ]
        real_btn = random.choice(verify_code)
        print(real_btn)
        verify_code.remove(real_btn)
        verbox = (random.randint(1, 3), random.randint(1, 3))
        buttons = []
        linebox = []
        was_set = False
        for x in range(3):
            x += 1
            for y in range(3):
                y += 1
                if verbox[0] == y and verbox[1] == x:
                    was_set = True
                    print("button was set to " + real_btn + "at " +
                          str(verbox[0]) + " - " + str(verbox[1]))
                    linebox.append(
                        InlineKeyboardButton(
                            text=real_btn,
                            callback_data="verify_me(y|{}|{})".format(
                                user_id, chat_id)))
                else:
                    verify_emoji = random.choice(verify_code)
                    linebox.append(
                        InlineKeyboardButton(
                            text=verify_emoji,
                            callback_data="verify_me(n|{}|{})".format(
                                user_id, chat_id)))
                    verify_code.remove(verify_emoji)
            buttons.append(linebox)
            linebox = []
        if not was_set:
            verbox = (random.randint(1, 3), random.randint(1, 3))
            linebox[verbox[0] - 1][verbox[1] - 1] = InlineKeyboardButton(
                text=real_btn,
                callback_data="verify_me(y|{}|{})".format(user_id, chat_id))
            print("[x] button was set to " + real_btn + "at " +
                  str(verbox[0]) + " - " + str(verbox[1]))
        buttons.append([
            InlineKeyboardButton(text="Refresh",
                                 callback_data="verify_me(re|{}|{})".format(
                                     user_id, chat_id))
        ])
        # query.message.delete()
        # context.bot.send_photo(chat.id, photo=open("emilia/modules/helper_funcs/emojis/" + verify_code_images[real_btn], 'rb'), caption=tl(update.effective_message, "Tolong pilih emoji yang sama dibawah ini:") + "\n" + tl(update.effective_message, "Jika tidak ada emoji yang sama, harap klik tombol refresh"), parse_mode="markdown", reply_markup=InlineKeyboardMarkup(buttons))
        context.bot.edit_message_media(
            chat.id,
            message_id=query.message.message_id,
            media=InputMediaPhoto(
                media=open(
                    "emilia/modules/helper_funcs/emojis/" +
                    verify_code_images[real_btn], 'rb'),
                caption=tl(update.effective_message,
                           "Tolong pilih emoji yang sama dibawah ini:") +
                "\n" + tl(
                    update.effective_message,
                    "Jika tidak ada emoji yang sama, harap klik tombol refresh"
                ),
                parse_mode="markdown"),
            reply_markup=InlineKeyboardMarkup(buttons))
        query.answer(text=tl(update.effective_message, "Done"),
                     show_alert=False)
    else:
        # query.message.delete()
        # context.bot.send_photo(chat.id, photo=open("emilia/modules/helper_funcs/emojis/fail.jpg", 'rb'), caption=tl(update.effective_message, "Maaf robot, kamu telah salah klik tombol verifikasi.\n\nCoba lagi dengan klik tombol verifikasi pada pesan selamat datang."), parse_mode="markdown")
        context.bot.edit_message_media(
            chat.id,
            message_id=query.message.message_id,
            media=InputMediaPhoto(
                media=open("emilia/modules/helper_funcs/emojis/fail.jpg",
                           'rb'),
                caption=tl(
                    update.effective_message,
                    "Maaf robot, kamu telah salah klik tombol verifikasi.\n\nCoba lagi dengan klik tombol verifikasi pada pesan selamat datang."
                ),
                parse_mode="markdown"))
        query.answer(text=tl(
            update.effective_message,
            "Gagal! Kamu telah salah mengklik tombol verifikasi"),
                     show_alert=True)
Beispiel #4
0
def new_member(bot: Bot, update: Update):
	chat = update.effective_chat  # type: Optional[Chat]

	should_welc, cust_welcome, cust_content, welc_type = sql.get_welc_pref(chat.id)
	cleanserv = sql.clean_service(chat.id)
	if cleanserv:
		new_members = update.effective_message.new_chat_members
		for new_mem in new_members:
			try:
				dispatcher.bot.delete_message(chat.id, update.message.message_id)
			except BadRequest:
				pass
	if should_welc:
		sent = None
		new_members = update.effective_message.new_chat_members
		for new_mem in new_members:
			# Give the owner a special welcome
			if OWNER_SPECIAL and new_mem.id == OWNER_ID:
				if cleanserv:
					bot.send_message(chat.id, tl(update.effective_message, "Master telah pulang! Mari kita mulai pesta ini! 😆"))
				else:
					send_message(update.effective_message, tl(update.effective_message, "Master telah pulang! Mari kita mulai pesta ini! 😆"))
				continue

			# Don't welcome yourself
			elif new_mem.id == bot.id:
				continue

			else:
				# If welcome message is media, send with appropriate function
				if welc_type != sql.Types.TEXT and welc_type != sql.Types.BUTTON_TEXT:
					reply = update.message.message_id
					# Clean service welcome
					if cleanserv:
						reply = False
					# Formatting text
					first_name = new_mem.first_name or "PersonWithNoName"  # edge case of empty name - occurs for some bugs.
					if new_mem.last_name:
						fullname = "{} {}".format(first_name, new_mem.last_name)
					else:
						fullname = first_name
					count = chat.get_members_count()
					mention = mention_markdown(new_mem.id, first_name)
					if new_mem.username:
						username = "******" + escape_markdown(new_mem.username)
					else:
						username = mention
					if cust_welcome:
						formatted_text = cust_welcome.format(first=escape_markdown(first_name),
											  last=escape_markdown(new_mem.last_name or first_name),
											  fullname=escape_markdown(fullname), username=username, mention=mention,
											  count=count, chatname=escape_markdown(chat.title), id=new_mem.id)
					else:
						formatted_text = ""
					# Build keyboard
					buttons = sql.get_welc_buttons(chat.id)
					keyb = build_keyboard(buttons)
					getsec, mutetime, custom_text = sql.welcome_security(chat.id)

					# If user ban protected don't apply security on him
					if is_user_ban_protected(chat, new_mem.id, chat.get_member(new_mem.id)):
						pass
					elif getsec:
						# If mute time is turned on
						if mutetime:
							if mutetime[:1] == "0":
								try:
									bot.restrict_chat_member(chat.id, new_mem.id, can_send_messages=False)
									canrest = True
								except BadRequest:
									canrest = False
							else:
								mutetime = extract_time(update.effective_message, mutetime)
								try:
									bot.restrict_chat_member(chat.id, new_mem.id, until_date=mutetime, can_send_messages=False)
									canrest = True
								except BadRequest:
									canrest = False
						# If security welcome is turned on
						if canrest:
							sql.add_to_userlist(chat.id, new_mem.id)
							keyb.append([InlineKeyboardButton(text=str(custom_text), callback_data="check_bot_({})".format(new_mem.id))])
					keyboard = InlineKeyboardMarkup(keyb)
					# Send message
					try:
						sent = ENUM_FUNC_MAP[welc_type](chat.id, cust_content, caption=formatted_text, reply_markup=keyboard, parse_mode="markdown", reply_to_message_id=reply)
					except BadRequest:
						sent = send_message(update.effective_message, tl(update.effective_message, "Catatan: Terjadi kesalahan saat mengirim pesan kustom. Harap perbarui."))
					return
				else:
					# else, move on
					first_name = new_mem.first_name or "PersonWithNoName"  # edge case of empty name - occurs for some bugs.

					if cust_welcome:
						if new_mem.last_name:
							fullname = "{} {}".format(first_name, new_mem.last_name)
						else:
							fullname = first_name
						count = chat.get_members_count()
						mention = mention_markdown(new_mem.id, first_name)
						if new_mem.username:
							username = "******" + escape_markdown(new_mem.username)
						else:
							username = mention

						valid_format = escape_invalid_curly_brackets(cust_welcome, VALID_WELCOME_FORMATTERS)
						if valid_format:
							res = valid_format.format(first=escape_markdown(first_name),
												  last=escape_markdown(new_mem.last_name or first_name),
												  fullname=escape_markdown(fullname), username=username, mention=mention,
												  count=count, chatname=escape_markdown(chat.title), id=new_mem.id)
						else:
							res = ""
						buttons = sql.get_welc_buttons(chat.id)
						keyb = build_keyboard(buttons)
					else:
						res = sql.DEFAULT_WELCOME.format(first=first_name)
						keyb = []

					getsec, mutetime, custom_text = sql.welcome_security(chat.id)
					
					# If user ban protected don't apply security on him
					if is_user_ban_protected(chat, new_mem.id, chat.get_member(new_mem.id)):
						pass
					elif getsec:
						if mutetime:
							if mutetime[:1] == "0":
								try:
									bot.restrict_chat_member(chat.id, new_mem.id, can_send_messages=False)
									canrest = True
								except BadRequest:
									canrest = False
							else:
								mutetime = extract_time(update.effective_message, mutetime)
								try:
									bot.restrict_chat_member(chat.id, new_mem.id, until_date=mutetime, can_send_messages=False)
									canrest = True
								except BadRequest:
									canrest = False
						if canrest:
							sql.add_to_userlist(chat.id, new_mem.id)
							keyb.append([InlineKeyboardButton(text=str(custom_text), callback_data="check_bot_({})".format(new_mem.id))])
					keyboard = InlineKeyboardMarkup(keyb)

					sent = send(update, res, keyboard,
								sql.DEFAULT_WELCOME.format(first=first_name))  # type: Optional[Message]

				
			prev_welc = sql.get_clean_pref(chat.id)
			if prev_welc:
				try:
					if int(prev_welc) != 1:
						bot.delete_message(chat.id, prev_welc)
				except BadRequest as excp:
				   pass

				if sent:
					sql.set_clean_welcome(chat.id, sent.message_id)

	fed_id = fedsql.get_fed_id(chat.id)
	if fed_id == "TeamNusantaraDevs":
		new_members = update.effective_message.new_chat_members
		for new_mem in new_members:
			# CAS Security thread
			t = threading.Thread(target=check_cas, args=(bot, new_mem.id, new_mem, update.effective_message,))
			t.start()