inlinefuncs.py 25 KB

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