inlinefuncs.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. """
  2. MIT License
  3. Copyright (c) 2021 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. import asyncio
  21. import os
  22. import sys
  23. from html import escape
  24. from re import sub as re_sub
  25. from sys import version as pyver
  26. from time import ctime, time
  27. from fuzzysearch import find_near_matches
  28. from motor import version as mongover
  29. from pykeyboard import InlineKeyboard
  30. from pyrogram import __version__ as pyrover
  31. from pyrogram import filters
  32. from pyrogram.raw.functions import Ping
  33. from pyrogram.types import (InlineKeyboardButton,
  34. InlineQueryResultArticle,
  35. InlineQueryResultPhoto,
  36. InputTextMessageContent)
  37. from search_engine_parser import GoogleSearch
  38. from wbb import (BOT_USERNAME, MESSAGE_DUMP_CHAT, SUDOERS, USERBOT_ID,
  39. USERBOT_NAME, USERBOT_USERNAME, app,
  40. app2, arq)
  41. from wbb.core.types import InlineQueryResultCachedDocument
  42. from wbb.modules.info import get_chat_info, get_user_info
  43. from wbb.modules.music import download_youtube_audio
  44. from wbb.utils.functions import test_speedtest
  45. from wbb.utils.pastebin import paste
  46. keywords_list = [
  47. "image",
  48. "wall",
  49. "tmdb",
  50. "lyrics",
  51. "exec",
  52. "speedtest",
  53. "search",
  54. "ping",
  55. "tr",
  56. "ud",
  57. "yt",
  58. "info",
  59. "google",
  60. "torrent",
  61. "wiki",
  62. "music",
  63. "ytmusic",
  64. ]
  65. async def inline_help_func(__HELP__):
  66. buttons = InlineKeyboard(row_width=4)
  67. buttons.add(
  68. *[
  69. (
  70. InlineKeyboardButton(
  71. text=i, switch_inline_query_current_chat=i
  72. )
  73. )
  74. for i in keywords_list
  75. ]
  76. )
  77. answerss = [
  78. InlineQueryResultArticle(
  79. title="Inline Commands",
  80. description="Help Related To Inline Usage.",
  81. input_message_content=InputTextMessageContent(
  82. "Click A Button To Get Started."
  83. ),
  84. thumb_url="https://hamker.me/cy00x5x.png",
  85. reply_markup=buttons,
  86. ),
  87. InlineQueryResultArticle(
  88. title="Github Repo",
  89. description="Get Github Respository Of Bot.",
  90. input_message_content=InputTextMessageContent(
  91. "https://github.com/thehamkercat/WilliamButcherBot"
  92. ),
  93. thumb_url="https://hamker.me/gjc9fo3.png",
  94. ),
  95. ]
  96. answerss = await alive_function(answerss)
  97. return answerss
  98. async def alive_function(answers):
  99. buttons = InlineKeyboard(row_width=2)
  100. bot_state = "Dead" if not await app.get_me() else "Alive"
  101. ubot_state = "Dead" if not await app2.get_me() else "Alive"
  102. buttons.add(
  103. InlineKeyboardButton("Stats", callback_data="stats_callback"),
  104. InlineKeyboardButton(
  105. "Go Inline!", switch_inline_query_current_chat=""
  106. ),
  107. )
  108. msg = f"""
  109. **[William✨](https://github.com/thehamkercat/WilliamButcherBot):**
  110. **MainBot:** `{bot_state}`
  111. **UserBot:** `{ubot_state}`
  112. **Python:** `{pyver.split()[0]}`
  113. **Pyrogram:** `{pyrover}`
  114. **MongoDB:** `{mongover}`
  115. **Platform:** `{sys.platform}`
  116. **Profiles:** [BOT](t.me/{BOT_USERNAME}) | [UBOT](t.me/{USERBOT_USERNAME})
  117. """
  118. answers.append(
  119. InlineQueryResultArticle(
  120. title="Alive",
  121. description="Check Bot's Stats",
  122. thumb_url="https://static2.aniimg.com/upload/20170515/414/c/d/7/cd7EEF.jpg",
  123. input_message_content=InputTextMessageContent(
  124. msg, disable_web_page_preview=True
  125. ),
  126. reply_markup=buttons,
  127. )
  128. )
  129. return answers
  130. async def translate_func(answers, lang, tex):
  131. result = await arq.translate(tex, lang)
  132. if not result.ok:
  133. answers.append(
  134. InlineQueryResultArticle(
  135. title="Error",
  136. description=result.result,
  137. input_message_content=InputTextMessageContent(
  138. result.result
  139. ),
  140. )
  141. )
  142. return answers
  143. result = result.result
  144. msg = f"""
  145. __**Translated from {result.src} to {result.dest}**__
  146. **INPUT:**
  147. {tex}
  148. **OUTPUT:**
  149. {result.translatedText}"""
  150. answers.extend(
  151. [
  152. InlineQueryResultArticle(
  153. title=f"Translated from {result.src} to {result.dest}.",
  154. description=result.translatedText,
  155. input_message_content=InputTextMessageContent(msg),
  156. ),
  157. InlineQueryResultArticle(
  158. title=result.translatedText,
  159. input_message_content=InputTextMessageContent(
  160. result.translatedText
  161. ),
  162. ),
  163. ]
  164. )
  165. return answers
  166. async def urban_func(answers, text):
  167. results = await arq.urbandict(text)
  168. if not results.ok:
  169. answers.append(
  170. InlineQueryResultArticle(
  171. title="Error",
  172. description=results.result,
  173. input_message_content=InputTextMessageContent(
  174. results.result
  175. ),
  176. )
  177. )
  178. return answers
  179. results = results.result[0:48]
  180. for i in results:
  181. clean = lambda x: re_sub(r"[\[\]]", "", x)
  182. msg = f"""
  183. **Query:** {text}
  184. **Definition:** __{clean(i.definition)}__
  185. **Example:** __{clean(i.example)}__"""
  186. answers.append(
  187. InlineQueryResultArticle(
  188. title=i.word,
  189. description=clean(i.definition),
  190. input_message_content=InputTextMessageContent(msg),
  191. )
  192. )
  193. return answers
  194. async def google_search_func(answers, text):
  195. gresults = await GoogleSearch().async_search(text)
  196. limit = 0
  197. for i in gresults:
  198. if limit > 48:
  199. break
  200. limit += 1
  201. try:
  202. msg = f"""
  203. [{i['titles']}]({i['links']})
  204. {i['descriptions']}"""
  205. answers.append(
  206. InlineQueryResultArticle(
  207. title=i["titles"],
  208. description=i["descriptions"],
  209. input_message_content=InputTextMessageContent(
  210. msg, disable_web_page_preview=True
  211. ),
  212. )
  213. )
  214. except KeyError:
  215. pass
  216. return answers
  217. async def wall_func(answers, text):
  218. results = await arq.wall(text)
  219. if not results.ok:
  220. answers.append(
  221. InlineQueryResultArticle(
  222. title="Error",
  223. description=results.result,
  224. input_message_content=InputTextMessageContent(
  225. results.result
  226. ),
  227. )
  228. )
  229. return answers
  230. results = results.result[0:48]
  231. for i in results:
  232. answers.append(
  233. InlineQueryResultPhoto(
  234. photo_url=i.url_image,
  235. thumb_url=i.url_thumb,
  236. caption=f"[Source]({i.url_image})",
  237. )
  238. )
  239. return answers
  240. async def torrent_func(answers, text):
  241. results = await arq.torrent(text)
  242. if not results.ok:
  243. answers.append(
  244. InlineQueryResultArticle(
  245. title="Error",
  246. description=results.result,
  247. input_message_content=InputTextMessageContent(
  248. results.result
  249. ),
  250. )
  251. )
  252. return answers
  253. results = results.result[0:48]
  254. for i in results:
  255. title = i.name
  256. size = i.size
  257. seeds = i.seeds
  258. leechs = i.leechs
  259. upload_date = i.uploaded
  260. magnet = i.magnet
  261. caption = f"""
  262. **Title:** __{title}__
  263. **Size:** __{size}__
  264. **Seeds:** __{seeds}__
  265. **Leechs:** __{leechs}__
  266. **Uploaded:** __{upload_date}__
  267. **Magnet:** `{magnet}`"""
  268. description = f"{size} | {upload_date} | Seeds: {seeds}"
  269. answers.append(
  270. InlineQueryResultArticle(
  271. title=title,
  272. description=description,
  273. input_message_content=InputTextMessageContent(
  274. caption, disable_web_page_preview=True
  275. ),
  276. )
  277. )
  278. pass
  279. return answers
  280. async def youtube_func(answers, text):
  281. results = await arq.youtube(text)
  282. if not results.ok:
  283. answers.append(
  284. InlineQueryResultArticle(
  285. title="Error",
  286. description=results.result,
  287. input_message_content=InputTextMessageContent(
  288. results.result
  289. ),
  290. )
  291. )
  292. return answers
  293. results = results.result[0:48]
  294. for i in results:
  295. buttons = InlineKeyboard(row_width=1)
  296. video_url = f"https://youtube.com{i.url_suffix}"
  297. buttons.add(InlineKeyboardButton("Watch", url=video_url))
  298. caption = f"""
  299. **Title:** {i.title}
  300. **Views:** {i.views}
  301. **Channel:** {i.channel}
  302. **Duration:** {i.duration}
  303. **Uploaded:** {i.publish_time}
  304. **Description:** {i.long_desc}"""
  305. description = f"{i.views} | {i.channel} | {i.duration} | {i.publish_time}"
  306. answers.append(
  307. InlineQueryResultArticle(
  308. title=i.title,
  309. thumb_url=i.thumbnails[0],
  310. description=description,
  311. input_message_content=InputTextMessageContent(
  312. caption, disable_web_page_preview=True
  313. ),
  314. reply_markup=buttons,
  315. )
  316. )
  317. return answers
  318. async def lyrics_func(answers, text):
  319. song = await arq.lyrics(text)
  320. if not song.ok:
  321. answers.append(
  322. InlineQueryResultArticle(
  323. title="Error",
  324. description=song.result,
  325. input_message_content=InputTextMessageContent(
  326. song.result
  327. ),
  328. )
  329. )
  330. return answers
  331. lyrics = song.result
  332. song = lyrics.splitlines()
  333. song_name = song[0]
  334. artist = song[1]
  335. if len(lyrics) > 4095:
  336. lyrics = await paste(lyrics)
  337. lyrics = f"**LYRICS_TOO_LONG:** [URL]({lyrics})"
  338. msg = f"__{lyrics}__"
  339. answers.append(
  340. InlineQueryResultArticle(
  341. title=song_name,
  342. description=artist,
  343. input_message_content=InputTextMessageContent(msg),
  344. )
  345. )
  346. return answers
  347. async def tg_search_func(answers, text, user_id):
  348. if user_id not in SUDOERS:
  349. msg = "**ERROR**\n__THIS FEATURE IS ONLY FOR SUDO USERS__"
  350. answers.append(
  351. InlineQueryResultArticle(
  352. title="ERROR",
  353. description="THIS FEATURE IS ONLY FOR SUDO USERS",
  354. input_message_content=InputTextMessageContent(msg),
  355. )
  356. )
  357. return answers
  358. if str(text)[-1] != ":":
  359. msg = "**ERROR**\n__Put A ':' After The Text To Search__"
  360. answers.append(
  361. InlineQueryResultArticle(
  362. title="ERROR",
  363. description="Put A ':' After The Text To Search",
  364. input_message_content=InputTextMessageContent(msg),
  365. )
  366. )
  367. return answers
  368. text = text[0:-1]
  369. async for message in app2.search_global(text, limit=49):
  370. buttons = InlineKeyboard(row_width=2)
  371. buttons.add(
  372. InlineKeyboardButton(
  373. text="Origin",
  374. url=message.link
  375. if message.link
  376. else "https://t.me/telegram",
  377. ),
  378. InlineKeyboardButton(
  379. text="Search again",
  380. switch_inline_query_current_chat="search",
  381. ),
  382. )
  383. name = (
  384. message.from_user.first_name
  385. if message.from_user.first_name
  386. else "NO NAME"
  387. )
  388. caption = f"""
  389. **Query:** {text}
  390. **Name:** {str(name)} [`{message.from_user.id}`]
  391. **Chat:** {str(message.chat.title)} [`{message.chat.id}`]
  392. **Date:** {ctime(message.date)}
  393. **Text:** >>
  394. {message.text.markdown if message.text else message.caption if message.caption else '[NO_TEXT]'}
  395. """
  396. result = InlineQueryResultArticle(
  397. title=name,
  398. description=message.text if message.text else "[NO_TEXT]",
  399. reply_markup=buttons,
  400. input_message_content=InputTextMessageContent(
  401. caption, disable_web_page_preview=True
  402. ),
  403. )
  404. answers.append(result)
  405. return answers
  406. async def music_inline_func(answers, query):
  407. chat_id = -1001445180719
  408. group_invite = "https://t.me/joinchat/vSDE2DuGK4Y4Nzll"
  409. try:
  410. messages = [
  411. m
  412. async for m in app2.search_messages(
  413. chat_id, query, filter="audio", limit=100
  414. )
  415. ]
  416. except Exception as e:
  417. print(e)
  418. msg = f"You Need To Join Here With Your Bot And Userbot To Get Cached Music.\n{group_invite}"
  419. answers.append(
  420. InlineQueryResultArticle(
  421. title="ERROR",
  422. description="Click Here To Know More.",
  423. input_message_content=InputTextMessageContent(
  424. msg, disable_web_page_preview=True
  425. ),
  426. )
  427. )
  428. return answers
  429. messages_ids_and_duration = []
  430. for f_ in messages:
  431. messages_ids_and_duration.append(
  432. {
  433. "message_id": f_.message_id,
  434. "duration": f_.audio.duration
  435. if f_.audio.duration
  436. else 0,
  437. }
  438. )
  439. messages = list(
  440. {v["duration"]: v for v in messages_ids_and_duration}.values()
  441. )
  442. messages_ids = [ff_["message_id"] for ff_ in messages]
  443. messages = await app.get_messages(chat_id, messages_ids[0:48])
  444. return [
  445. InlineQueryResultCachedDocument(
  446. file_id=message_.audio.file_id,
  447. title=message_.audio.title,
  448. )
  449. for message_ in messages
  450. ]
  451. async def wiki_func(answers, text):
  452. data = await arq.wiki(text)
  453. if not data.ok:
  454. answers.append(
  455. InlineQueryResultArticle(
  456. title="Error",
  457. description=data.result,
  458. input_message_content=InputTextMessageContent(
  459. data.result
  460. ),
  461. )
  462. )
  463. return answers
  464. data = data.result
  465. msg = f"""
  466. **QUERY:**
  467. {data.title}
  468. **ANSWER:**
  469. __{data.answer}__"""
  470. answers.append(
  471. InlineQueryResultArticle(
  472. title=data.title,
  473. description=data.answer,
  474. input_message_content=InputTextMessageContent(msg),
  475. )
  476. )
  477. return answers
  478. async def speedtest_init(query):
  479. answers = []
  480. user_id = query.from_user.id
  481. if user_id not in SUDOERS:
  482. msg = "**ERROR**\n__THIS FEATURE IS ONLY FOR SUDO USERS__"
  483. answers.append(
  484. InlineQueryResultArticle(
  485. title="ERROR",
  486. description="THIS FEATURE IS ONLY FOR SUDO USERS",
  487. input_message_content=InputTextMessageContent(msg),
  488. )
  489. )
  490. return answers
  491. msg = "**Click The Button Below To Perform A Speedtest**"
  492. button = InlineKeyboard(row_width=1)
  493. button.add(
  494. InlineKeyboardButton(
  495. text="Test", callback_data="test_speedtest"
  496. )
  497. )
  498. answers.append(
  499. InlineQueryResultArticle(
  500. title="Click Here",
  501. input_message_content=InputTextMessageContent(msg),
  502. reply_markup=button,
  503. )
  504. )
  505. return answers
  506. """ callback query for the function above """
  507. @app.on_callback_query(filters.regex("test_speedtest"))
  508. async def test_speedtest_cq(_, cq):
  509. if cq.from_user.id not in SUDOERS:
  510. return await cq.answer("This Isn't For You!")
  511. inline_message_id = cq.inline_message_id
  512. await app.edit_inline_text(inline_message_id, "**Testing**")
  513. loop = asyncio.get_running_loop()
  514. download, upload, info = await loop.run_in_executor(
  515. None, test_speedtest
  516. )
  517. msg = f"""
  518. **Download:** `{download}`
  519. **Upload:** `{upload}`
  520. **Latency:** `{info['latency']} ms`
  521. **Country:** `{info['country']} [{info['cc']}]`
  522. **Latitude:** `{info['lat']}`
  523. **Longitude:** `{info['lon']}`
  524. """
  525. await app.edit_inline_text(inline_message_id, msg)
  526. async def pmpermit_func(answers, user_id, victim):
  527. if user_id != USERBOT_ID:
  528. return
  529. caption = f"Hi, I'm {USERBOT_NAME}, What are you here for?, You'll be blocked if you send more than 5 messages."
  530. buttons = InlineKeyboard(row_width=2)
  531. buttons.add(
  532. InlineKeyboardButton(
  533. text="To Scam You", callback_data="pmpermit to_scam_you a"
  534. ),
  535. InlineKeyboardButton(
  536. text="For promotion",
  537. callback_data="pmpermit to_scam_you a",
  538. ),
  539. InlineKeyboardButton(
  540. text="Approve me", callback_data="pmpermit approve_me a"
  541. ),
  542. InlineKeyboardButton(
  543. text="Approve", callback_data=f"pmpermit approve {victim}"
  544. ),
  545. InlineKeyboardButton(
  546. text="Block & Delete",
  547. callback_data=f"pmpermit block {victim}",
  548. ),
  549. )
  550. answers.append(
  551. InlineQueryResultArticle(
  552. title="do_not_click_here",
  553. reply_markup=buttons,
  554. input_message_content=InputTextMessageContent(caption),
  555. )
  556. )
  557. return answers
  558. async def ping_func(answers):
  559. ping = Ping(ping_id=app.rnd_id())
  560. t1 = time()
  561. await app.send(ping)
  562. t2 = time()
  563. ping = f"{str(round((t2 - t1) * 1000, 2))} ms"
  564. answers.append(
  565. InlineQueryResultArticle(
  566. title=ping,
  567. input_message_content=InputTextMessageContent(
  568. f"__**{ping}**__"
  569. ),
  570. )
  571. )
  572. return answers
  573. async def yt_music_func(answers, url):
  574. if "http" not in url:
  575. url = (await arq.youtube(url)).result[0]
  576. url = f"https://youtube.com{url.url_suffix}"
  577. loop = asyncio.get_running_loop()
  578. music = await loop.run_in_executor(
  579. None, download_youtube_audio, url
  580. )
  581. if not music:
  582. msg = "**ERROR**\n__MUSIC TOO LONG__"
  583. answers.append(
  584. InlineQueryResultArticle(
  585. title="ERROR",
  586. description="MUSIC TOO LONG",
  587. input_message_content=InputTextMessageContent(msg),
  588. )
  589. )
  590. return answers
  591. (
  592. title,
  593. performer,
  594. duration,
  595. audio,
  596. thumbnail,
  597. ) = music
  598. m = await app.send_audio(
  599. MESSAGE_DUMP_CHAT,
  600. audio,
  601. title=title,
  602. duration=duration,
  603. performer=performer,
  604. thumb=thumbnail,
  605. )
  606. os.remove(audio)
  607. os.remove(thumbnail)
  608. answers.append(
  609. InlineQueryResultCachedDocument(
  610. title=title, file_id=m.audio.file_id
  611. )
  612. )
  613. return answers
  614. async def info_inline_func(answers, peer):
  615. not_found = InlineQueryResultArticle(
  616. title="PEER NOT FOUND",
  617. input_message_content=InputTextMessageContent(
  618. "PEER NOT FOUND"
  619. ),
  620. )
  621. try:
  622. user = await app.get_users(peer)
  623. caption, _ = await get_user_info(user, True)
  624. except IndexError:
  625. try:
  626. chat = await app.get_chat(peer)
  627. caption, _ = await get_chat_info(chat, True)
  628. except Exception:
  629. return [not_found]
  630. except Exception:
  631. return [not_found]
  632. answers.append(
  633. InlineQueryResultArticle(
  634. title="Found Peer.",
  635. input_message_content=InputTextMessageContent(
  636. caption, disable_web_page_preview=True
  637. ),
  638. )
  639. )
  640. return answers
  641. async def tmdb_func(answers, query):
  642. response = await arq.tmdb(query)
  643. if not response.ok:
  644. answers.append(
  645. InlineQueryResultArticle(
  646. title="Error",
  647. description=response.result,
  648. input_message_content=InputTextMessageContent(
  649. response.result
  650. ),
  651. )
  652. )
  653. return answers
  654. results = response.result[:49]
  655. for result in results:
  656. if not result.poster and not result.backdrop:
  657. continue
  658. if not result.genre:
  659. genre = None
  660. else:
  661. genre = " | ".join(result.genre)
  662. description = (
  663. result.overview[0:900] if result.overview else "None"
  664. )
  665. caption = f"""
  666. **{result.title}**
  667. **Type:** {result.type}
  668. **Rating:** {result.rating}
  669. **Genre:** {genre}
  670. **Release Date:** {result.releaseDate}
  671. **Description:** __{description}__
  672. """
  673. buttons = InlineKeyboard(row_width=1)
  674. buttons.add(
  675. InlineKeyboardButton(
  676. "Search Again",
  677. switch_inline_query_current_chat="tmdb",
  678. )
  679. )
  680. answers.append(
  681. InlineQueryResultPhoto(
  682. photo_url=result.backdrop
  683. if result.backdrop
  684. else result.poster,
  685. caption=caption,
  686. title=result.title,
  687. description=f"{genre} • {result.releaseDate} • {result.rating} • {description}",
  688. reply_markup=buttons,
  689. )
  690. )
  691. return answers
  692. async def image_func(answers, query):
  693. results = await arq.image(query)
  694. if not results.ok:
  695. answers.append(
  696. InlineQueryResultArticle(
  697. title="Error",
  698. description=results.result,
  699. input_message_content=InputTextMessageContent(
  700. results.result
  701. ),
  702. )
  703. )
  704. return answers
  705. results = results.result[:49]
  706. buttons = InlineKeyboard(row_width=2)
  707. buttons.add(
  708. InlineKeyboardButton(
  709. text="Search again",
  710. switch_inline_query_current_chat="image",
  711. ),
  712. )
  713. for i in results:
  714. answers.append(
  715. InlineQueryResultPhoto(
  716. title=i.title,
  717. photo_url=i.url,
  718. thumb_url=i.url,
  719. reply_markup=buttons,
  720. )
  721. )
  722. return answers
  723. async def execute_code(query):
  724. text = query.query.strip()
  725. offset = int((query.offset or 0))
  726. answers = []
  727. languages = (await arq.execute()).result
  728. if len(text.split()) == 1:
  729. answers = [
  730. InlineQueryResultArticle(
  731. title=lang,
  732. input_message_content=InputTextMessageContent(lang),
  733. )
  734. for lang in languages
  735. ][offset : offset + 25]
  736. await query.answer(
  737. next_offset=str(offset + 25),
  738. results=answers,
  739. cache_time=1,
  740. )
  741. elif len(text.split()) == 2:
  742. text = text.split()[1].strip()
  743. languages = list(
  744. filter(
  745. lambda x: find_near_matches(text, x, max_l_dist=1),
  746. languages,
  747. )
  748. )
  749. answers.extend(
  750. [
  751. InlineQueryResultArticle(
  752. title=lang,
  753. input_message_content=InputTextMessageContent(
  754. lang
  755. ),
  756. )
  757. for lang in languages
  758. ][:49]
  759. )
  760. else:
  761. lang = text.split()[1]
  762. code = text.split(None, 2)[2]
  763. response = await arq.execute(lang, code)
  764. if not response.ok:
  765. answers.append(
  766. InlineQueryResultArticle(
  767. title="Error",
  768. input_message_content=InputTextMessageContent(
  769. response.result
  770. ),
  771. )
  772. )
  773. else:
  774. res = response.result
  775. stdout, stderr = escape(res.stdout), escape(res.stderr)
  776. output = stdout or stderr
  777. out = (
  778. "STDOUT"
  779. if stdout
  780. else ("STDERR" if stderr else "No output")
  781. )
  782. msg = f"""
  783. **{lang.capitalize()}:**
  784. ```{code}```
  785. **{out}:**
  786. ```{output}```
  787. """
  788. answers.append(
  789. InlineQueryResultArticle(
  790. title="Executed",
  791. description=output[:20],
  792. input_message_content=InputTextMessageContent(
  793. msg
  794. ),
  795. )
  796. )
  797. await query.answer(results=answers, cache_time=1)