bot.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import telebot
  2. import re
  3. TOKEN = ''
  4. bot = telebot.TeleBot(TOKEN)
  5. receiveReference = False
  6. reference = ''
  7. pattern = "http[s]?:\/\/(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+"
  8. refPattern = "ref=.*\?"
  9. @bot.message_handler(commands=['start'])
  10. def send_welcome(message):
  11. bot.reply_to(message, "Hello, I'm ready.")
  12. @bot.message_handler(commands=['help'])
  13. def send_help(message):
  14. bot.reply_to(message, "My commands are: /start, /help, /reference")
  15. @bot.message_handler(commands=['reference'])
  16. def send_question(message):
  17. global receiveReference
  18. receiveReference = True
  19. bot.reply_to(message, "Tell me, what's your reference code?.")
  20. @bot.message_handler()
  21. def process_response(message):
  22. global receiveReference, reference
  23. if(len(reference) > 0):
  24. result = re.match(pattern, message.text)
  25. if(result):
  26. url = result.group()
  27. print("[INFO] Found URL match on: %s" % url)
  28. refResult = re.search(refPattern, url)
  29. if(refResult):
  30. ref = refResult.group()
  31. print("[INFO] Found reference match on: %s" % ref)
  32. newMessage = message.text.replace(ref, "ref=%s?" % reference)
  33. bot.send_message(message.chat.id, newMessage)
  34. bot.delete_message(chat_id=message.chat.id, message_id=message.message_id)
  35. else:
  36. print("[INFO] No reference pattern matched")
  37. else:
  38. print("[INFO] No URL pattern matched")
  39. elif(receiveReference):
  40. reference = message.text
  41. bot.reply_to(message, "OK, your reference is: %s, send me now any URL." % reference)
  42. receiveReference = False
  43. else:
  44. print("[INFO] Receive reference disabled, try /reference")
  45. try:
  46. print('[INFO] Bot started...')
  47. bot.polling()
  48. except Exception as ex:
  49. print("Something goes wrong, exception: ", ex)