inlinefuncs.py 26 KB

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