inline.py 5.6 KB

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