From 90c6e8af8c7b825c42142b5578a23291797940f4 Mon Sep 17 00:00:00 2001 From: yasirarism Date: Mon, 18 Sep 2023 11:41:50 +0700 Subject: [PATCH] Calc Feature, still buggy for inline (#284) --- misskaty/plugins/inline_search.py | 35 +++++++++- misskaty/plugins/misc_tools.py | 104 ++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 1 deletion(-) diff --git a/misskaty/plugins/inline_search.py b/misskaty/plugins/inline_search.py index 2d0ac648..7990caf4 100644 --- a/misskaty/plugins/inline_search.py +++ b/misskaty/plugins/inline_search.py @@ -28,6 +28,7 @@ from pyrogram.types import ( from misskaty import BOT_USERNAME, app, user from misskaty.helper import GENRES_EMOJI, fetch, post_to_telegraph, search_jw from misskaty.plugins.dev import shell_exec +from misskaty.plugins.misc_tools import calc_btn from misskaty.vars import USER_SESSION from utils import demoji @@ -41,7 +42,7 @@ To use this feature, just type bot username with following args below. ~ info [user id/username] - Check info about a user. """ -keywords_list = ["imdb", "pypi", "git", "google", "secretmsg", "info", "botapi"] +keywords_list = ["imdb", "pypi", "git", "google", "calc", "secretmsg", "info", "botapi"] PRVT_MSGS = {} LOGGER = getLogger("MissKaty") @@ -108,6 +109,38 @@ async def inline_menu(self, inline_query: InlineQuery): ), ] await inline_query.answer(results=answerss) + elif inline_query.query.strip().lower().split()[0] == "calc": + if len(inline_query.query.strip().lower().split()) < 2: + answers = [ + InlineQueryResultArticle( + title="Calculator", + description="New Calculator", + input_message_content=InputTextMessageContent( + message_text=f"Made by @{self.me.username}", + disable_web_page_preview=True + ), + reply_markup=calc_btn(inline_query.from_user.id) + ) + ] + else: + data = inline_query.query.replace("×", "*").replace("÷", "/") + result = str(eval(text)) + answers = [ + InlineQueryResultArticle( + title="Answer", + description=f"Result: {result}", + input_message_content=InputTextMessageContent( + message_text=f"{data} = {result}", + disable_web_page_preview=True + ) + ) + ] + await inline_query.answer( + results=answers, + is_gallery=False, + is_personal=False, + switch_pm_parameter="help", + ) elif inline_query.query.strip().lower().split()[0] == "botapi": if len(inline_query.query.strip().lower().split()) < 2: return await inline_query.answer( diff --git a/misskaty/plugins/misc_tools.py b/misskaty/plugins/misc_tools.py index 2ad1c3d1..236c89a4 100644 --- a/misskaty/plugins/misc_tools.py +++ b/misskaty/plugins/misc_tools.py @@ -9,6 +9,7 @@ import asyncio import html import json import os +import re import traceback from logging import getLogger from urllib.parse import quote @@ -45,6 +46,7 @@ LOGGER = getLogger("MissKaty") __MODULE__ = "Misc" __HELP__ = """ /carbon [text or reply to text or caption] - Make beautiful snippet code on carbon from text. +/calc - Simple math calculator using inline buttons. /kbbi [keyword] - Search definition on KBBI (For Indonesian People) /sof [query] - Search your problem in StackOverflow. /google [query] - Search using Google Search. @@ -67,6 +69,108 @@ def remove_html_tags(text): return re.sub(clean, "", text) +def calcExpression(text): + try: + return float(eval(text)) + except (SyntaxError, ZeroDivisionError): + return "" + except TypeError: + return float(eval(text.replace('(', '*('))) + except Exception as e: + logger.error(e, exc_info=True) + return "" + + +def calc_btn(uid): + CALCULATE_BUTTONS = InlineKeyboardMarkup( + [ + [ + InlineKeyboardButton("DEL", callback_data=f"calc|{uid}|DEL"), + InlineKeyboardButton("AC", callback_data=f"calc|{uid}|AC"), + InlineKeyboardButton("(", callback_data=f"calc|{uid}|("), + InlineKeyboardButton(")", callback_data=f"calc|{uid}|)") + ], + [ + InlineKeyboardButton("7", callback_data=f"calc|{uid}|7"), + InlineKeyboardButton("8", callback_data=f"calc|{uid}|8"), + InlineKeyboardButton("9", callback_data=f"calc|{uid}|9"), + InlineKeyboardButton("÷", callback_data=f"calc|{uid}|/") + ], + [ + InlineKeyboardButton("4", callback_data=f"calc|{uid}|4"), + InlineKeyboardButton("5", callback_data=f"calc|{uid}|5"), + InlineKeyboardButton("6", callback_data=f"calc|{uid}|6"), + InlineKeyboardButton("×", callback_data=f"calc|{uid}|*") + ], + [ + InlineKeyboardButton("1", callback_data=f"calc|{uid}|1"), + InlineKeyboardButton("2", callback_data=f"calc|{uid}|2"), + InlineKeyboardButton("3", callback_data=f"calc|{uid}|3"), + InlineKeyboardButton("-", callback_data=f"calc|{uid}|-"), + ], + [ + InlineKeyboardButton(".", callback_data=f"calc|{uid}|."), + InlineKeyboardButton("0", callback_data=f"calc|{uid}|0"), + InlineKeyboardButton("=", callback_data=f"calc|{uid}|="), + InlineKeyboardButton("+", callback_data=f"calc|{uid}|+"), + ] + ] + ) + return CALCULATE_BUTTONS + + +@app.on_message(filters.command(["calc", "calculate", "calculator"])) +async def calculate_handler(self, ctx): + if not ctx.from_user: + return + await ctx.reply_text( + text=f"Made by @{self.me.username}", + reply_markup=calc_btn(ctx.from_user.id), + disable_web_page_preview=True, + quote=True + ) + +@app.on_callback_query(filters.regex("^calc")) +async def calc_cb(self, query): + _, uid, data = query.data.split("|") + if query.from_user.id != int(uid): + return await query.answer("Who are you??", show_alert=True, cache_time=5) + try: + text = query.message.text.split("\n")[0].strip().split("=")[0].strip() + text = '' if f"Made by @{self.me.username}" in text else text + inpt = text + query.data + result = "" + if data == "=": + result = calcExpression(text) + text = "" + elif data == "DEL": + text = text[:-1] + elif data == "AC": + text = "" + else: + dot_dot_check = re.findall(r"(\d*\.\.|\d*\.\d+\.)", inpt) + opcheck = re.findall(r"([*/\+-]{2,})", inpt) + if not dot_dot_check and not opcheck: + strOperands = re.findall(r"(\.\d+|\d+\.\d+|\d+)", inpt) + if strOperands: + text += data + result = calcExpression(text) + + text = f"{text:<50}" + if result: + if text: + text += f"\n{result:>50}" + else: + text = result + text += f"\n\nMade by @{self.me.username}" + await query.message.edit_msg( + text=text, + disable_web_page_preview=True, + reply_markup=calc_btn(query.from_user.id) + ) + except Exception as error: + LOGGER.error(error) + @app.on_cmd("kbbi") async def kbbi_search(_, ctx: Client): if len(ctx.command) == 1: