automatization.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import asyncio
  2. import re
  3. import time
  4. from settings import AD_MESSAGE, STANDART_DELAY, ACTIVATE_SENDING_MESSAGE, MAX_FAILURES, ACTIVATE_BOTS_MESSAGE, \
  5. BOT_MESSAGE, CHANNEL_MESSAGE, ACTIVATE_GROUPS_MESSAGE, GROUP_MESSAGE
  6. from Console import console
  7. from loader import Browser
  8. from database import add_group, add_group_account_connection, delete_old_connections
  9. from telethon.tl.functions.channels import JoinChannelRequest, LeaveChannelRequest
  10. from random import randint
  11. async def add_clicking(client, bot_chat, BOT_NAME, http_prx):
  12. # setting some variables for control
  13. failures = 0
  14. url = " "
  15. pr_url = "some text"
  16. first_time = True
  17. skipped = False
  18. await client.send_message(BOT_NAME, ACTIVATE_SENDING_MESSAGE)
  19. time.sleep(3)
  20. while True:
  21. messages = await client.get_messages(bot_chat, limit=1)
  22. for message in messages:
  23. # message.message is a text in current message
  24. if bool(re.search(AD_MESSAGE, message.message)):
  25. url = client.get_url_from_button(message)
  26. console.send_message(url)
  27. if url == pr_url:
  28. time.sleep(15)
  29. await message.click(2) # clicking on a third button in message
  30. console.send_message("Skiping")
  31. skipped = True
  32. else:
  33. try:
  34. Browser.GetUrl(url, use_old_browser=first_time or skipped,
  35. PROXY=http_prx, delay=STANDART_DELAY)
  36. except Exception as e:
  37. console.send_alert("Something wrong with browser: {}".format(e))
  38. if first_time:
  39. first_time = False
  40. if skipped:
  41. skipped = False
  42. pr_url = url
  43. time.sleep(3)
  44. elif re.search("Sorry, this task is", message.message):
  45. await client.send_message(BOT_NAME, "/visit")
  46. elif re.search("Sorry, there are no new ads available.", message.message):
  47. """
  48. sleep_for = randint(10, 60)
  49. console.send_notification("No new ads, sleeping for {} minutes".format(sleep_for))
  50. time.sleep(60 * sleep_for)
  51. await client.send_message(BOT_NAME, ACTIVATE_SENDING_MESSAGE)
  52. """
  53. return
  54. else:
  55. failures += 1
  56. console.send_notification("Something went wrong, waiting...")
  57. time.sleep(300)
  58. await client.send_message(BOT_NAME, ACTIVATE_SENDING_MESSAGE)
  59. time.sleep(10)
  60. if failures > MAX_FAILURES:
  61. console.send_notification("Exiting because of max failures")
  62. return
  63. async def find_and_forward(client, bot_chat, bot_to_activate):
  64. try:
  65. await client.send_message(bot_to_activate[0], '/start')
  66. except Exception as e:
  67. console.send_alert("Message wasn't sent due to error: {}".format(e))
  68. return
  69. time.sleep(2)
  70. dialogs = await client.get_dialogs()
  71. bot_to_activate_chat = None
  72. for dlg in dialogs:
  73. if dlg.title == bot_to_activate[1]:
  74. bot_to_activate_chat = dlg
  75. messages = await client.get_messages(bot_to_activate_chat, limit=1)
  76. for message in messages:
  77. if message.text != '/start':
  78. await client.forward_messages(bot_chat, message)
  79. time.sleep(2)
  80. time.sleep(3)
  81. await client.delete_dialog(bot_to_activate_chat)
  82. async def send_to_bots(client, bot_chat, BOT_NAME, http_prx):
  83. await client.send_message(BOT_NAME, ACTIVATE_BOTS_MESSAGE)
  84. time.sleep(3)
  85. failures = 0
  86. url = " "
  87. pr_url = "some text"
  88. first_time = False
  89. skipped = False
  90. bot_to_activate = None
  91. while True:
  92. messages = await client.get_messages(bot_chat, limit=1)
  93. for message in messages:
  94. if bool(re.search(BOT_MESSAGE, message.message)):
  95. url = client.get_url_from_button(message)
  96. console.send_message(url)
  97. if url == pr_url:
  98. await message.click(2) # clicking on a third button in message
  99. console.send_message("Skiping")
  100. skipped = True
  101. time.sleep(2)
  102. else:
  103. try:
  104. bot_to_activate = Browser.GetBotUrl(url, use_old_browser=first_time or skipped,
  105. PROXY=http_prx, delay=STANDART_DELAY)
  106. except Exception as e:
  107. console.send_alert("Something wrong with browser: {}".format(e))
  108. if first_time:
  109. first_time = False
  110. if skipped:
  111. skipped = False
  112. pr_url = url
  113. time.sleep(3)
  114. elif re.search("Sorry, this task is", message.message):
  115. await client.send_message(BOT_NAME, "/bots")
  116. elif re.search("Sorry, there are no new", message.message):
  117. return
  118. else:
  119. failures += 1
  120. console.send_message(message.message)
  121. console.send_alert("Something went wrong, waiting...")
  122. time.sleep(300)
  123. await client.send_message(BOT_NAME, ACTIVATE_SENDING_MESSAGE)
  124. time.sleep(10)
  125. if failures > MAX_FAILURES:
  126. console.send_alert("Exiting because of max failures")
  127. return
  128. if bot_to_activate[0]:
  129. await find_and_forward(client, bot_chat, bot_to_activate)
  130. async def _join_group(client, bot_chat, group_to_join, message):
  131. try:
  132. await client(JoinChannelRequest(channel=group_to_join[1]))
  133. except Exception as e:
  134. console.send_critical("Error when joining chanel: {}".format(e))
  135. time.sleep(3)
  136. try:
  137. await message.click(1)
  138. except Exception as e:
  139. console.send_alert("Error with clicking on button: {}".format(e))
  140. time.sleep(1)
  141. add_group(group_to_join[0], group_to_join[1])
  142. messages = await client.get_messages(bot_chat, limit=2)
  143. stay_in_group = 1
  144. for el in messages:
  145. if "Success" in el.message:
  146. pos = el.message.index("at least")
  147. stay_in_group = int(el.message[pos + 9:el.message[pos + 9:].index(' ') + pos + 9])
  148. add_group_account_connection(client, group_to_join[1], stay_in_group)
  149. time.sleep(2)
  150. async def leave_all_old_groups(client):
  151. groups_to_leave = delete_old_connections(client)
  152. dialogs = await client.get_dialogs()
  153. bot_to_activate_chat = None
  154. for dlg in dialogs:
  155. if dlg.title in groups_to_leave:
  156. await client.delete_dialog(dlg)
  157. time.sleep(1)
  158. async def add_to_the_group(client, bot_chat, BOT_NAME, http_prx):
  159. await client.send_message(BOT_NAME, ACTIVATE_GROUPS_MESSAGE)
  160. time.sleep(3)
  161. failures = 0
  162. url = " "
  163. pr_url = "some text"
  164. first_time = False
  165. skipped = False
  166. tries = 0
  167. group_to_join = [None, None]
  168. while True:
  169. console.send_message(tries)
  170. messages = await client.get_messages(bot_chat, limit=1)
  171. for message in messages:
  172. if re.search(CHANNEL_MESSAGE, message.message) or re.search(GROUP_MESSAGE, message.message):
  173. url = client.get_url_from_button(message)
  174. console.send_message(url)
  175. if url != pr_url:
  176. tries = 0
  177. if url == pr_url and tries > 1:
  178. await message.click(3) # clicking on a fourth button in message
  179. console.send_message("Skiping")
  180. skipped = True
  181. tries = 0
  182. time.sleep(2)
  183. else:
  184. try:
  185. group_to_join = Browser.GetGroupUrl(url, use_old_browser=first_time or skipped,
  186. PROXY=http_prx, delay=STANDART_DELAY)
  187. except Exception as e:
  188. console.send_alert("Something wrong with browser: {}".format(e))
  189. if first_time:
  190. first_time = False
  191. if skipped:
  192. skipped = False
  193. pr_url = url
  194. tries += 1
  195. time.sleep(3)
  196. elif re.search("Sorry, this task is", message.message):
  197. await client.send_message(BOT_NAME, "/bots")
  198. elif re.search("Sorry, there are no new", message.message):
  199. return
  200. elif re.search("We cannot find you in the channel", message.message):
  201. tries = 0 #just retrying
  202. else:
  203. failures += 1
  204. console.send_alert("Something went wrong, waiting...")
  205. console.send_message(message.message)
  206. time.sleep(300)
  207. await client.send_message(BOT_NAME, ACTIVATE_GROUPS_MESSAGE)
  208. time.sleep(10)
  209. if failures > MAX_FAILURES:
  210. console.send_alert("Exiting because of max failures")
  211. return
  212. if group_to_join[0] and group_to_join[1]:
  213. await _join_group(client, bot_chat, group_to_join, message)
  214. async def begin(client, bot):
  215. await client.send_message(bot, ACTIVATE_SENDING_MESSAGE)
  216. time.sleep(randint(1000, 4000)/1000)
  217. await client.send_message(bot, ACTIVATE_BOTS_MESSAGE)
  218. time.sleep(randint(1000, 4000)/1000)
  219. await client.send_message(bot, ACTIVATE_GROUPS_MESSAGE)
  220. time.sleep(randint(4000, 8000)/1000)