inlinefuncs.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. import aiohttp
  2. import json
  3. import sys
  4. import asyncio
  5. from pyrogram.types import (
  6. InlineQueryResultArticle,
  7. InputTextMessageContent,
  8. InlineKeyboardButton,
  9. InlineQueryResultPhoto
  10. )
  11. from googletrans import Translator
  12. from search_engine_parser import GoogleSearch
  13. from pykeyboard import InlineKeyboard
  14. from sys import version as pyver
  15. from motor import version as mongover
  16. from pyrogram import __version__ as pyrover
  17. from time import time, ctime
  18. from wbb.modules.userbot import eval_executor_func
  19. from wbb.utils.fetch import fetch
  20. from wbb.utils.formatter import convert_seconds_to_minutes as time_convert
  21. from wbb.utils.pastebin import paste
  22. from wbb.core.types.InlineQueryResult import InlineQueryResultCachedDocument
  23. from wbb import (
  24. arq, app, app2, USERBOT_USERNAME,
  25. BOT_USERNAME, LOG_GROUP_ID, USERBOT_ID
  26. )
  27. async def inline_help_func(__HELP__):
  28. buttons = InlineKeyboard(row_width=2)
  29. buttons.add(
  30. InlineKeyboardButton(
  31. 'Get More Help.',
  32. url=f"t.me/{BOT_USERNAME}?start=start"
  33. ),
  34. InlineKeyboardButton(
  35. "Go Inline!",
  36. switch_inline_query_current_chat=""
  37. )
  38. )
  39. answerss = [
  40. InlineQueryResultArticle(
  41. title="Inline Commands",
  42. description="Help Related To Inline Usage.",
  43. input_message_content=InputTextMessageContent(__HELP__),
  44. thumb_url="https://hamker.me/cy00x5x.png",
  45. reply_markup=buttons
  46. )
  47. ]
  48. answerss = await alive_function(answerss)
  49. return answerss
  50. async def alive_function(answers):
  51. buttons = InlineKeyboard(row_width=2)
  52. bot_state = 'Dead' if not await app.get_me() else 'Alive'
  53. ubot_state = 'Dead' if not await app2.get_me() else 'Alive'
  54. buttons.add(
  55. InlineKeyboardButton(
  56. 'Stats',
  57. callback_data='stats_callback'
  58. ),
  59. InlineKeyboardButton(
  60. "Go Inline!",
  61. switch_inline_query_current_chat=""
  62. )
  63. )
  64. msg = f"""
  65. **[William✨](https://github.com/thehamkercat/WilliamButcherBot):**
  66. **MainBot:** `{bot_state}`
  67. **UserBot:** `{ubot_state}`
  68. **Python:** `{pyver.split()[0]}`
  69. **Pyrogram:** `{pyrover}`
  70. **MongoDB:** `{mongover}`
  71. **Platform:** `{sys.platform}`
  72. **Profiles:** [BOT](t.me/{BOT_USERNAME}) | [UBOT](t.me/{USERBOT_USERNAME})
  73. """
  74. answers.append(
  75. InlineQueryResultArticle(
  76. title='Alive',
  77. description="Check Bot's Stats",
  78. thumb_url="https://static2.aniimg.com/upload/20170515/414/c/d/7/cd7EEF.jpg",
  79. input_message_content=InputTextMessageContent(
  80. msg,
  81. disable_web_page_preview=True
  82. ),
  83. reply_markup=buttons,
  84. )
  85. )
  86. return answers
  87. async def translate_func(answers, lang, tex):
  88. i = Translator().translate(tex, dest=lang)
  89. msg = f"""
  90. __**Translated to {lang}**__
  91. **INPUT:**
  92. {tex}
  93. **OUTPUT:**
  94. {i.text}"""
  95. answers.append(
  96. InlineQueryResultArticle(
  97. title=f'Translated to {lang}',
  98. description=i.text,
  99. input_message_content=InputTextMessageContent(msg)
  100. )
  101. )
  102. return answers
  103. async def urban_func(answers, text):
  104. results = await arq.urbandict(text)
  105. limit = 0
  106. for i in results:
  107. if limit > 48:
  108. break
  109. limit += 1
  110. try:
  111. msg = f"""
  112. **Query:** {text}
  113. **Definition:** __{results[i].definition}__
  114. **Example:** __{results[i].example}__"""
  115. answers.append(
  116. InlineQueryResultArticle(
  117. title=results[i].word,
  118. description=results[i].definition,
  119. input_message_content=InputTextMessageContent(msg)
  120. ))
  121. except KeyError:
  122. pass
  123. return answers
  124. async def google_search_func(answers, text):
  125. gresults = await GoogleSearch().async_search(text)
  126. limit = 0
  127. for i in gresults:
  128. if limit > 48:
  129. break
  130. limit += 1
  131. try:
  132. msg = f"""
  133. [{i['titles']}]({i['links']})
  134. {i['descriptions']}"""
  135. answers.append(
  136. InlineQueryResultArticle(
  137. title=i['titles'],
  138. description=i['descriptions'],
  139. input_message_content=InputTextMessageContent(
  140. msg,
  141. disable_web_page_preview=True
  142. )
  143. ))
  144. except KeyError:
  145. pass
  146. return answers
  147. async def wall_func(answers, text):
  148. results = await arq.wall(text)
  149. limit = 0
  150. for i in results:
  151. if limit > 48:
  152. break
  153. limit += 1
  154. try:
  155. answers.append(
  156. InlineQueryResultPhoto(
  157. photo_url=results[i].url_image,
  158. thumb_url=results[i].url_thumb,
  159. caption=f"[Source]({results[i].url_image})"
  160. ))
  161. except KeyError:
  162. pass
  163. return answers
  164. async def saavn_func(answers, text):
  165. buttons_list = []
  166. results = await arq.saavn(text)
  167. for i in results:
  168. buttons = InlineKeyboard(row_width=1)
  169. buttons.add(
  170. InlineKeyboardButton(
  171. 'Download | Play',
  172. url=results[i].media_url
  173. )
  174. )
  175. buttons_list.append(buttons)
  176. duration = await time_convert(results[i].duration)
  177. caption = f"""
  178. **Title:** {results[i].song}
  179. **Album:** {results[i].album}
  180. **Duration:** {duration}
  181. **Release:** {results[i].year}
  182. **Singers:** {results[i].singers}"""
  183. description = f"{results[i].album} | {duration} " \
  184. + f"| {results[i].singers} ({results[i].year})"
  185. try:
  186. answers.append(
  187. InlineQueryResultArticle(
  188. title=results[i].song,
  189. input_message_content=InputTextMessageContent(
  190. caption,
  191. disable_web_page_preview=True
  192. ),
  193. description=description,
  194. thumb_url=results[i].image,
  195. reply_markup=buttons_list[i]
  196. ))
  197. except (KeyError, ValueError):
  198. pass
  199. return answers
  200. async def deezer_func(answers, text):
  201. buttons_list = []
  202. results = await arq.deezer(text, 5)
  203. for i in results:
  204. buttons = InlineKeyboard(row_width=1)
  205. buttons.add(
  206. InlineKeyboardButton(
  207. 'Download | Play',
  208. url=results[i].url
  209. )
  210. )
  211. buttons_list.append(buttons)
  212. duration = await time_convert(results[i].duration)
  213. caption = f"""
  214. **Title:** {results[i].title}
  215. **Artist:** {results[i].artist}
  216. **Duration:** {duration}
  217. **Source:** [Deezer]({results[i].source})"""
  218. description = f"{results[i].artist} | {duration}"
  219. try:
  220. answers.append(
  221. InlineQueryResultArticle(
  222. title=results[i].title,
  223. thumb_url=results[i].thumbnail,
  224. description=description,
  225. input_message_content=InputTextMessageContent(
  226. caption,
  227. disable_web_page_preview=True
  228. ),
  229. reply_markup=buttons_list[i]
  230. ))
  231. except (KeyError, ValueError):
  232. pass
  233. return answers
  234. async def webss(url):
  235. start_time = time()
  236. if "." not in url:
  237. return
  238. screenshot = await fetch(f"https://patheticprogrammers.cf/ss?site={url}")
  239. end_time = time()
  240. try:
  241. m = await app.send_photo(LOG_GROUP_ID, photo=screenshot['url'])
  242. except TypeError:
  243. return
  244. await m.delete()
  245. a = []
  246. pic = InlineQueryResultPhoto(
  247. photo_url=screenshot['url'],
  248. caption=(f"`{url}`\n__Took {round(end_time - start_time)} Seconds.__")
  249. )
  250. a.append(pic)
  251. return a
  252. # Used my api key here, don't fuck with it
  253. async def shortify(url):
  254. if "." not in url:
  255. return
  256. header = {
  257. "Authorization": "Bearer ad39983fa42d0b19e4534f33671629a4940298dc",
  258. 'Content-Type': 'application/json'
  259. }
  260. payload = {
  261. "long_url": f"{url}"
  262. }
  263. payload = json.dumps(payload)
  264. async with aiohttp.ClientSession() as session:
  265. async with session.post("https://api-ssl.bitly.com/v4/shorten", headers=header, data=payload) as resp:
  266. data = await resp.json()
  267. msg = f"**Original Url:** {url}\n**Shortened Url:** {data['link']}"
  268. a = []
  269. b = InlineQueryResultArticle(
  270. title="Link Shortened!",
  271. description=data['link'],
  272. input_message_content=InputTextMessageContent(
  273. msg, disable_web_page_preview=True)
  274. )
  275. a.append(b)
  276. return a
  277. async def torrent_func(answers, text):
  278. results = await arq.torrent(text)
  279. limit = 0
  280. for i in results:
  281. if limit > 48:
  282. break
  283. title = results[i].name
  284. size = results[i].size
  285. seeds = results[i].seeds
  286. leechs = results[i].leechs
  287. upload_date = results[i].uploaded + " Ago"
  288. magnet = results[i].magnet
  289. caption = f"""
  290. **Title:** __{title}__
  291. **Size:** __{size}__
  292. **Seeds:** __{seeds}__
  293. **Leechs:** __{leechs}__
  294. **Uploaded:** __{upload_date}__
  295. **Magnet:** `{magnet}`"""
  296. description = f"{size} | {upload_date} | Seeds: {seeds}"
  297. try:
  298. answers.append(
  299. InlineQueryResultArticle(
  300. title=title,
  301. description=description,
  302. input_message_content=InputTextMessageContent(
  303. caption,
  304. disable_web_page_preview=True
  305. )
  306. )
  307. )
  308. limit += 1
  309. except (KeyError, ValueError):
  310. pass
  311. return answers
  312. async def youtube_func(answers, text):
  313. results = await arq.youtube(text)
  314. limit = 0
  315. for i in results:
  316. if limit > 48:
  317. break
  318. limit += 1
  319. buttons = InlineKeyboard(row_width=1)
  320. video_url = f"https://youtube.com{results[i].url_suffix}"
  321. buttons.add(
  322. InlineKeyboardButton(
  323. 'Watch',
  324. url=video_url
  325. )
  326. )
  327. caption = f"""
  328. **Title:** {results[i].title}
  329. **Views:** {results[i].views}
  330. **Channel:** {results[i].channel}
  331. **Duration:** {results[i].duration}
  332. **Uploaded:** {results[i].publish_time}
  333. **Description:** {results[i].long_desc}"""
  334. description = f"{results[i].views} | {results[i].channel} | " \
  335. + f"{results[i].duration} | {results[i].publish_time}"
  336. try:
  337. answers.append(
  338. InlineQueryResultArticle(
  339. title=results[i].title,
  340. thumb_url=results[i].thumbnails[0],
  341. description=description,
  342. input_message_content=InputTextMessageContent(
  343. caption,
  344. disable_web_page_preview=True
  345. ),
  346. reply_markup=buttons
  347. ))
  348. except (KeyError, ValueError):
  349. pass
  350. return answers
  351. async def lyrics_func(answers, text):
  352. song = await arq.lyrics(text)
  353. lyrics = song.lyrics
  354. song = lyrics.splitlines()
  355. song_name = song[0]
  356. artist = song[1]
  357. if len(lyrics) > 4095:
  358. lyrics = await paste(lyrics)
  359. lyrics = f"**LYRICS_TOO_LONG:** [URL]({lyrics})"
  360. msg = f"__{lyrics}__"
  361. answers.append(
  362. InlineQueryResultArticle(
  363. title=song_name,
  364. description=artist,
  365. input_message_content=InputTextMessageContent(msg)
  366. ))
  367. return answers
  368. async def eval_func(answers, text, user_id):
  369. if user_id != USERBOT_ID:
  370. msg = "**ERROR**\n__THIS FEATURE IS ONLY FOR SUDO USERS__"
  371. answers.append(
  372. InlineQueryResultArticle(
  373. title="ERROR",
  374. description="THIS FEATURE IS ONLY FOR SUDO USERS",
  375. input_message_content=InputTextMessageContent(msg)
  376. ))
  377. return answers
  378. if str(text)[-1] != ":":
  379. msg = "**ERROR**\n__Put A ':' After The Code To Execute It__"
  380. answers.append(
  381. InlineQueryResultArticle(
  382. title="ERROR",
  383. description="Put A ':' After The Code To Execute It",
  384. input_message_content=InputTextMessageContent(msg)
  385. ))
  386. return answers
  387. text = text[0:-1]
  388. start_time = time()
  389. result = await eval_executor_func(text)
  390. output = result[1]
  391. result = result[0]
  392. msg = f"{result}"
  393. end_time = time()
  394. answers.append(
  395. InlineQueryResultArticle(
  396. title=f"Took {round(end_time - start_time)} Seconds.",
  397. description=output,
  398. input_message_content=InputTextMessageContent(msg)
  399. ))
  400. return answers
  401. async def github_user_func(answers, text):
  402. URL = f"https://api.github.com/users/{text}"
  403. result = await fetch(URL)
  404. buttons = InlineKeyboard(row_width=1)
  405. buttons.add(InlineKeyboardButton(
  406. text="Open On Github",
  407. url=f"https://github.com/{text}"
  408. ))
  409. caption = f"""
  410. **Info Of {result['name']}**
  411. **Username:** `{text}`
  412. **Bio:** `{result['bio']}`
  413. **Profile Link:** [Here]({result['html_url']})
  414. **Company:** `{result['company']}`
  415. **Created On:** `{result['created_at']}`
  416. **Repositories:** `{result['public_repos']}`
  417. **Blog:** `{result['blog']}`
  418. **Location:** `{result['location']}`
  419. **Followers:** `{result['followers']}`
  420. **Following:** `{result['following']}`"""
  421. answers.append(
  422. InlineQueryResultPhoto(
  423. photo_url=result['avatar_url'],
  424. caption=caption,
  425. reply_markup=buttons
  426. ))
  427. return answers
  428. async def github_repo_func(answers, text):
  429. text = text.replace("https://github.com/", "")
  430. text = text.replace("http://github.com/", "")
  431. if text[-1] == "/":
  432. text = text[0:-1]
  433. URL = f"https://api.github.com/repos/{text}"
  434. URL2 = f"https://api.github.com/repos/{text}/contributors"
  435. results = await asyncio.gather(fetch(URL), fetch(URL2))
  436. r = results[0]
  437. r1 = results[1]
  438. commits = 0
  439. for developer in r1:
  440. commits += developer['contributions']
  441. buttons = InlineKeyboard(row_width=1)
  442. buttons.add(
  443. InlineKeyboardButton(
  444. 'Open On Github',
  445. url=f"https://github.com/{text}"
  446. )
  447. )
  448. caption = f"""
  449. **Info Of {r['full_name']}**
  450. **Stars:** `{r['stargazers_count']}`
  451. **Watchers:** `{r['watchers_count']}`
  452. **Forks:** `{r['forks_count']}`
  453. **Commits:** `{commits}`
  454. **Is Fork:** `{r['fork']}`
  455. **Language:** `{r['language']}`
  456. **Contributors:** `{len(r1)}`
  457. **License:** `{r['license']['name']}`
  458. **Repo Owner:** [{r['owner']['login']}]({r['owner']['html_url']})
  459. **Created On:** `{r['created_at']}`
  460. **Homepage:** {r['homepage']}
  461. **Description:** __{r['description']}__"""
  462. answers.append(
  463. InlineQueryResultArticle(
  464. title="Found Repo",
  465. description=text,
  466. reply_markup=buttons,
  467. input_message_content=InputTextMessageContent(
  468. caption,
  469. disable_web_page_preview=True
  470. )
  471. ))
  472. return answers
  473. async def tg_search_func(answers, text, user_id):
  474. if user_id != USERBOT_ID:
  475. msg = "**ERROR**\n__THIS FEATURE IS ONLY FOR SUDO USERS__"
  476. answers.append(
  477. InlineQueryResultArticle(
  478. title="ERROR",
  479. description="THIS FEATURE IS ONLY FOR SUDO USERS",
  480. input_message_content=InputTextMessageContent(msg)
  481. ))
  482. return answers
  483. if str(text)[-1] != ":":
  484. msg = "**ERROR**\n__Put A ':' After The Text To Search__"
  485. answers.append(
  486. InlineQueryResultArticle(
  487. title="ERROR",
  488. description="Put A ':' After The Text To Search",
  489. input_message_content=InputTextMessageContent(msg)
  490. ))
  491. return answers
  492. text = text[0:-1]
  493. async for message in app2.search_global(text, limit=49):
  494. buttons = InlineKeyboard(row_width=2)
  495. buttons.add(
  496. InlineKeyboardButton(
  497. text="Origin",
  498. url=message.link if message.link else "https://t.me/telegram"
  499. ),
  500. InlineKeyboardButton(
  501. text="Search again",
  502. switch_inline_query_current_chat="search"
  503. )
  504. )
  505. name = message.from_user.first_name if message.from_user.first_name else "NO NAME"
  506. caption = f"""
  507. **Query:** {text}
  508. **Name:** {str(name)} [`{message.from_user.id}`]
  509. **Chat:** {str(message.chat.title)} [`{message.chat.id}`]
  510. **Date:** {ctime(message.date)}
  511. **Text:** >>
  512. {message.text.markdown if message.text else message.caption if message.caption else '[NO_TEXT]'}
  513. """
  514. result = InlineQueryResultArticle(
  515. title=name,
  516. description=message.text if message.text else "[NO_TEXT]",
  517. reply_markup=buttons,
  518. input_message_content=InputTextMessageContent(
  519. caption,
  520. disable_web_page_preview=True
  521. )
  522. )
  523. answers.append(result)
  524. return answers
  525. async def music_inline_func(answers, query):
  526. chat_id = -1001445180719
  527. group_invite = "https://t.me/joinchat/vSDE2DuGK4Y4Nzll"
  528. try:
  529. messages = [m async for m in app2.search_messages(chat_id, query, filter="audio", limit=199)]
  530. except Exception as e:
  531. print(e)
  532. msg = f"You Need To Join Here With Your Bot And Userbot To Get Cached Music.\n{group_invite}"
  533. answers.append(
  534. InlineQueryResultArticle(
  535. title="ERROR",
  536. description="Click Here To Know More.",
  537. input_message_content=InputTextMessageContent(
  538. msg,
  539. disable_web_page_preview=True
  540. )
  541. )
  542. )
  543. return answers
  544. messages_ids_and_duration = []
  545. for f_ in messages:
  546. messages_ids_and_duration.append(
  547. {"message_id": f_.message_id, "duration": f_.audio.duration if f_.audio.duration else 0}
  548. )
  549. messages = list(
  550. {v["duration"]: v for v in messages_ids_and_duration}.values())
  551. messages_ids = []
  552. for ff_ in messages:
  553. messages_ids.append(ff_['message_id'])
  554. messages = await app.get_messages(chat_id, messages_ids[0:48])
  555. for message_ in messages:
  556. answers.append(
  557. InlineQueryResultCachedDocument(
  558. file_id=message_.audio.file_id,
  559. title=message_.audio.title
  560. )
  561. )
  562. return answers
  563. async def wiki_func(answers, text):
  564. data = await arq.wiki(text)
  565. msg = f"""
  566. **QUERY:**
  567. {data['title']}
  568. **ANSWER:**
  569. __{data['answer']}__"""
  570. answers.append(
  571. InlineQueryResultArticle(
  572. title=data['title'],
  573. description=data['answer'],
  574. input_message_content=InputTextMessageContent(msg)
  575. )
  576. )
  577. return answers