telegram bot - simeonlam/knowledge GitHub Wiki

create bot

  • open BotFather in telegram
  • command to create bot /newbot
  • input bot title
  • input bot username
  • store return HTTP API BOT_TOKEN [eg: 1234567890:AABBcdefgxxxxxxxxxx]
  • start chat with telegram bot http://t.me/{bot username}
  • Create new group and invite new create bot to share message to others
  • sent some message to new bot for prepare response of getUpdates to get chatId

use API to get status

curl "https://api.telegram.org/bot${BOT_TOKEN}/getUpdates"

.result[].my_chat_member:.chat.id
# group start with -

sendMessage

  • CHAT_ID: getUpdates response json location .result[0].message.chat.id
via POST API
curl -X "POST" "https://api.telegram.org/bot${BOT_TOKEN}/sendMessage" \
     -H 'Content-Type: application/json; charset=utf-8' \
     -d $'{
  "chat_id": "${CHAT_ID}",
  "text": "msg via api"
}'
post with groovy
def sendAlert = { msg ->
    def token = "xxxxxxxxx"
    def telegramURL = "https://api.telegram.org/$token/sendMessage"
    def chatId = "-xxxxxxxxxxx"
    
    sh """
    curl -d '{"chat_id": "${chatId}","text": "${msg}"}' -H 'Content-Type: application/json' -X POST ${telegramURL}
    """
}
via GET API
curl "https://api.telegram.org/bot${BOT_TOKEN}/sendMessage?chat_id=${CHAT_ID}&disable_web_page_preview=1&parse_mode=Markdown&text='test%20via%20get'"

reference