inline.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import sys
  2. from wbb import arq, app, app2, SUDOERS, USERBOT_USERNAME, BOT_USERNAME
  3. from wbb.utils.errors import capture_err
  4. from pykeyboard import InlineKeyboard
  5. from sys import version as pyver
  6. from motor import version as mongover
  7. from pyrogram import __version__ as pyrover
  8. from pyrogram import filters
  9. from pyrogram.types import (
  10. InlineQueryResultArticle,
  11. InputTextMessageContent,
  12. InlineKeyboardButton
  13. )
  14. from googletrans import Translator
  15. from search_engine_parser import GoogleSearch
  16. __MODULE__ = "Inline"
  17. __HELP__ = """
  18. tr [LANGUAGE_CODE] [QUERY] - Translate Text.
  19. ud [QUERY] - Urban Dictionary Query.
  20. google [Query] - Google Search.
  21. alive - Check Bot's Stats."""
  22. """ Inspiration From https://github.com/pokurt/Nana-Remix/blob/master/nana/plugins/assistant/inline_mod/alive.py """
  23. async def alive_function(answers):
  24. buttons = InlineKeyboard(row_width=1)
  25. bot_state = 'Dead' if not await app.get_me() else 'Alive'
  26. ubot_state = 'Dead' if not await app2.get_me() else 'Alive'
  27. buttons.add(InlineKeyboardButton('Stats', callback_data='stats_callback'))
  28. msg = f"""
  29. **[William✨](https://github.com/thehamkercat/WilliamButcherBot):**
  30. **MainBot:** `{bot_state}`
  31. **UserBot:** `{ubot_state}`
  32. **Python:** `{pyver.split()[0]}`
  33. **Pyrogram:** `{pyrover}`
  34. **MongoDB:** `{mongover}`
  35. **Platform:** `{sys.platform}`
  36. **Profiles:** [BOT](t.me/{BOT_USERNAME}) | [UBOT](t.me/{USERBOT_USERNAME})
  37. """
  38. answers.append(
  39. InlineQueryResultArticle(
  40. title='Alive',
  41. description="Check Bot's Stats",
  42. input_message_content=InputTextMessageContent(
  43. msg,
  44. disable_web_page_preview=True
  45. ),
  46. reply_markup=buttons,
  47. )
  48. )
  49. return answers
  50. async def translate_func(answers, lang, tex):
  51. i = Translator().translate(tex, dest=lang)
  52. msg = f"""
  53. __**Translated to {lang}**__
  54. **INPUT:**
  55. {tex}
  56. **OUTPUT:**
  57. {i.text}"""
  58. answers.append(
  59. InlineQueryResultArticle(
  60. title=f'Translated to {lang}',
  61. description=i.text,
  62. input_message_content=InputTextMessageContent(msg)
  63. )
  64. )
  65. return answers
  66. async def urban_func(answers, text):
  67. results = await arq.urbandict(text)
  68. for i in results:
  69. msg = f"""
  70. **Query:** {text}
  71. **Definition:** __{results[i].definition}__
  72. **Example:** __{results[i].example}__"""
  73. answers.append(
  74. InlineQueryResultArticle(
  75. title=results[i].word,
  76. description=results[i].definition,
  77. input_message_content=InputTextMessageContent(msg)
  78. ))
  79. return answers
  80. async def google_search_func(answers, text):
  81. gresults = await GoogleSearch().async_search(text)
  82. for i in gresults:
  83. try:
  84. msg = f"""
  85. [{i['titles']}]({i['links']})
  86. ```{i['descriptions']}```"""
  87. answers.append(
  88. InlineQueryResultArticle(
  89. title=i['titles'],
  90. description=i['descriptions'],
  91. input_message_content=InputTextMessageContent(
  92. msg,
  93. disable_web_page_preview=True
  94. )
  95. ))
  96. except KeyError:
  97. pass
  98. return answers
  99. @app.on_inline_query()
  100. async def inline_query_handler(client, query):
  101. try:
  102. text = query.query.lower()
  103. answers = []
  104. if text == '':
  105. await client.answer_inline_query(
  106. query.id,
  107. results=answers,
  108. switch_pm_text='Click here for help',
  109. switch_pm_parameter='help',
  110. )
  111. return
  112. elif text.split()[0] == "alive":
  113. answerss = await alive_function(answers)
  114. await client.answer_inline_query(
  115. query.id,
  116. results=answerss,
  117. cache_time=10
  118. )
  119. elif text.split()[0] == "tr":
  120. lang = text.split()[1]
  121. tex = text.split(None, 2)[2]
  122. answerss = await translate_func(answers, lang, tex)
  123. await client.answer_inline_query(
  124. query.id,
  125. results=answerss,
  126. cache_time=10
  127. )
  128. elif text.split()[0] == "ud":
  129. tex = text.split(None, 1)[1]
  130. answerss = await urban_func(answers, tex)
  131. await client.answer_inline_query(
  132. query.id,
  133. results=answerss,
  134. cache_time=10
  135. )
  136. elif text.split()[0] == "google":
  137. tex = text.split(None, 1)[1]
  138. answerss = await google_search_func(answers, tex)
  139. await client.answer_inline_query(
  140. query.id,
  141. results=answerss,
  142. cache_time=10
  143. )
  144. except IndexError:
  145. return