Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. import telebot
  2. from random import randint
  3. import pymysql
  4. from time import sleep
  5. bot = telebot.TeleBot('APIKEY')
  6.  
  7. pool = []
  8.  
  9.  
  10. def get_random_message(list):
  11. sleep(randint(2, 5))
  12. return list[randint(0, len(list) - 1)]
  13.  
  14.  
  15. def remove_symbols(msg):
  16. sym = '%\\;\''
  17. res = msg
  18. for i in msg:
  19. if i in sym:
  20. res = res.replace(i, '')
  21. return res
  22.  
  23.  
  24. def zhri_ego_eblo_tupoe(msg, chatid):
  25. insert = 'INSERT INTO data SET message=\'' + remove_symbols(msg) + '\', keywords=\''
  26. select = 'SELECT message FROM data WHERE '
  27. for i in msg.split(' '):
  28. if len(remove_symbols(i)) > 3:
  29. i_keys = remove_symbols(i).split(' ')
  30. if insert != 'INSERT INTO data SET message=\'' + remove_symbols(msg) + '\', keywords=\'':
  31. insert += ','
  32. if select != 'SELECT message FROM data WHERE ':
  33. select += ' OR '
  34. for k in i_keys:
  35. insert += k.lower()
  36. select += 'keywords LIKE \'%' + k.lower() + '%\''
  37. insert += '\''
  38.  
  39. print(insert)
  40. print(select)
  41. print('----------------------------------------------')
  42.  
  43. conn = pymysql.connect(host='127.0.0.1', port=3306, user='root',
  44. passwd='', db='rer', use_unicode=True, charset="utf8")
  45.  
  46. cur = conn.cursor()
  47. try:
  48. cur.execute(select)
  49. if cur.rowcount:
  50. answers = []
  51. for row in cur:
  52. answers.append(row[0])
  53. print(answers)
  54. try:
  55. bot.send_message(chatid, get_random_message(answers))
  56. except:
  57. pass
  58.  
  59. try:
  60. cur.execute(insert)
  61. conn.commit()
  62. except Exception as e:
  63. print('***********************************')
  64. print(e)
  65. print('***********************************')
  66.  
  67. except Exception as e:
  68.  
  69. print('***********************************')
  70.  
  71. print(e)
  72.  
  73. print('***********************************')
  74. finally:
  75. conn.close()
  76. cur.close()
  77.  
  78.  
  79.  
  80.  
  81.  
  82. @bot.message_handler(content_types=["text"])
  83. def handle_message(message):
  84. '''
  85.  
  86. bot.send_message(message.chat.id, get_random_message(pool))
  87. '''
  88. print('******')
  89. print('CHAT_ID: ' + str(message.chat.id))
  90. print('******')
  91. if '@' in message.text or len(message.text) > 300 or 'чилен' in message.text.lower():
  92. pass
  93. else:
  94. rnd = randint(0, 999)
  95. if rnd % 2 == 0:
  96. try:
  97. zhri_ego_eblo_tupoe(message.text, message.chat.id)
  98. except Exception as e:
  99. print(e)
  100. while True:
  101. try:
  102. bot.polling(none_stop=True)
  103. except Exception as e:
  104. print('***********************************')
  105. print(e)
  106. print('***********************************')
  107. sleep(15)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement