THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

test
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.1KB

  1. #!/bin/sh
  2. # Yona status telegram bot shell
  3. #
  4. # Check Yona server status and send me/group a message with telegram
  5. #
  6. # Configurations start..
  7. # ~~~~~~~~~~~~~~~~~~~~~~
  8. # API_TOKEN=123456789:AbCdEfgijk1LmPQRSTu234v5Wx-yZA67BCD
  9. API_TOKEN=""
  10. # USER_CHAT_ID=123456789
  11. USER_CHAT_ID=""
  12. # TARGET_SERVER="http://my-yona-server.com"
  13. TARGET_SERVER="http://127.0.0.1:9000"
  14. CHECK_URL="$TARGET_SERVER/-_-api/v1/hello"
  15. EXPECTED_STATUS_CODE=200
  16. # Polling Time (sec)
  17. POLLING_TIME=60
  18. # Messages
  19. messageOnDown="Yona is unavaliable!"
  20. messageOnRevive="Yona comeback!"
  21. # Configurations end here...
  22. #######################################
  23. if [ $# -eq 0 ]
  24. then
  25. if [ -z $API_TOKEN ]
  26. then
  27. echo "Telegram Bot api token is reqruied!"
  28. echo ""
  29. echo "ex>"
  30. echo "sh is-alive-bot.sh 328394984:AAFhL69afasfqjtUtIeRSzIagVYw7H3zF4"
  31. echo ""
  32. echo "See: https://core.telegram.org/bots#3-how-do-i-create-a-bot"
  33. exit 1
  34. fi
  35. else
  36. API_TOKEN=$1
  37. fi
  38. if [ -z $USER_CHAT_ID ]
  39. then
  40. if [ -z $2 ]
  41. then
  42. echo "---------------------------------------------------"
  43. echo "Find chat id of user or group with your own eyes.. "
  44. echo ""
  45. echo "..chat\":{ \"id\": 621884 ... <= user or group id!"
  46. echo "---------------------------------------------------"
  47. echo ""
  48. curl -sL https://api.telegram.org/bot$API_TOKEN/getUpdates
  49. echo ""
  50. echo "---------------------------------------------------"
  51. echo ""
  52. echo "and then retry..."
  53. echo "sh is-alive-bot.sh API_TOKEN USER_CHAT_ID"
  54. echo ""
  55. echo "ex>"
  56. echo "sh is-alive-bot.sh 328394984:AAFhL69afasfqjtUtIeRSzIagVYw7H3zF4 2156789"
  57. exit 1;
  58. else
  59. USER_CHAT_ID=$2
  60. fi
  61. fi
  62. ### preparing for message and server status check
  63. sendMessageToTelegram() {
  64. curl -s \
  65. -X POST \
  66. https://api.telegram.org/bot$API_TOKEN/sendMessage \
  67. -d text="$1" \
  68. -d chat_id=$USER_CHAT_ID > /dev/null
  69. }
  70. # it is used for one time message sending
  71. isDown=false
  72. echo ""
  73. echo "Monitoring started... $(date +%Y-%m-%d" "%H:%M:%S)"
  74. echo ""
  75. sendMessageToTelegram "Monitoring started... - $TARGET_SERVER"
  76. while true
  77. do
  78. NOW=$(date +%Y-%m-%d" "%H:%M:%S)
  79. response=$(curl -I $CHECK_URL 2> /dev/null | head -n 1 | cut -d$' ' -f2)
  80. if [ -z $response ] || [ $response != $EXPECTED_STATUS_CODE ]
  81. then
  82. if [ $isDown == false ]
  83. then
  84. echo "Sending message - $messageOnDown - $NOW"
  85. isDown=true
  86. sendMessageToTelegram "$messageOnDown $response - $TARGET_SERVER"
  87. fi
  88. else
  89. if [ $isDown == true ]
  90. then
  91. echo "Sending message - $messageOnRevive - $NOW"
  92. isDown=false
  93. sendMessageToTelegram "$messageOnRevive - $TARGET_SERVER"
  94. fi
  95. fi
  96. sleep $POLLING_TIME
  97. done;
  98. #
  99. # referred pages
  100. #
  101. # https://core.telegram.org/bots#3-how-do-i-create-a-bot
  102. # https://core.telegram.org/bots/api
  103. # https://community.onion.io/topic/499/sending-telegram-messages-via-bots
  104. #