inlinefuncs.py 25 KB

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