0. 安裝 telegram 設定帳號 UACCT
1. 加入 @BotFather
BotFather is the one bot to rule them all. Use it to create new bot accounts and manage your existing bots.
About Telegram bots:
https://core.telegram.org/bots
Bot API manual:
https://core.telegram.org/bots/api
Contact @BotSupport if you have questions about the Bot API.
UACCT:
/start
BotFather:
I can help you create and manage Telegram bots. If you're new to the Bot API, please see the manual.
You can control me by sending these commands:
/newbot - create a new bot
/mybots - edit your bots [beta]
/mygames - edit your games [beta]
...
2. 新增 bot
UACCT:
/newbot
BotFather:
Alright, a new bot. How are we going to call it? Please choose a name for your bot.
UACCT:
p****y
BotFather:
Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot.
UACCT:
botusername_bot
BotFather:
Done! Congratulations on your new bot. You will find it at t.me/pipboy88_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you've finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this.
Use this token to access the HTTP API:
5*******2:A*********************************w
For a description of the Bot API, see this page: https://core.telegram.org/bots/api
3. 測試 API
3.1. 查詢我是誰
https://api.telegram.org/bot5*******2:A*********************************w/getMe
回應
{"ok":true,"result":{"id":5*******2,"is_bot":true,"first_name":"p****y","username":"botusername_bot"}}
3.2. 加入 bot 聊天並取得聊天室編號
https://api.telegram.org/bot5*******2:A*********************************w/getUpdates
{
"ok": true,
"result": [
{
"update_id": 5*******0,
"message": {
"message_id": 41,
"from": {
"id": CHAT_ID,
"is_bot": false,
"first_name": "UACCT_DESCRIPTION",
"username": "UACCT",
"language_code": "zh-TW"
},
"chat": {
"id": CHAT_ID,
"first_name": "UACCT_DESCRIPTION",
"username": "UACCT",
"type": "private"
},
"date": 1524858545,
"text": "hbbvoebbvebwber"
}
}
]
}
3.3. 送出第一則訊息
https://api.telegram.org/bot5*******2:A*********************************w/sendMessage?chat_id=CHAT_ID&text=Hello+World
3.4. 送訊息 script
3.4.1. 先安裝 packages
apt-get install jq cconv fortune fortune-zh
3.4.2. 送訊息程式 tgsendmsg.sh
#!/usr/bin/env bash
PATH=$PATH
TS=`date +"%s.%N"`
LOGPATH="/PATH/to/tglog"
LOG="$LOGPATH/LogFilename.log"
BOTTK="5*******2:A*********************************w"
CHATID="CHAT_ID"
mkdir -p $LOGPATH
touch $LOG
chown -R 0755 $LOGPATH
if [ -n "$1" ]; then
MSG="$1"
shift
else
echo "Usage: $0 \"Message\""
exit 1
fi
TM=`curl --progress-bar --data-urlencode "chat_id=$CHATID" --data-urlencode "text=$MSG" "https://api.telegram.org/bot$BOTTK/sendMessage"`
echo "$TM" | jq
echo "$TS $TM" >> $LOG
3.4.3. 呼叫送出訊息 MyMSG.sh
#!/usr/bin/env bash
PATH=$PATH
Z="Asia/Taipei"
HR=`TZ=":$Z" date +"%H"`
HR=`echo "$HR" | sed 's/^0//'`
echo $HR
if [ $HR -eq 6 ]; then
/root/tgsendmsg.sh "Good Morning 早安"
FTZH=`/usr/games/fortune-zh | cconv -f UTF8-CN -t UTF8-TW`
MSG=""
for d in $FTZH
do
MSG="$MSG $d"
done
MSG=`echo "$MSG" | sed -r "s/\x1B\[[0-9;]*[mK]//g"`
/root/tgsendmsg.sh "$MSG"
fi
if [ $HR -eq 12 ]; then
/root/tgsendmsg.sh "Good Afternoon 午安"
FT=`/usr/games/fortune | cconv -f UTF8-CN -t UTF8-TW`
FT=`echo "$FT" | sed -r "s/\x1B\[[0-9;]*[mK]//g"`
/root/tgsendmsg.sh "$FT"
fi
if [ $HR -eq 18 ]; then
/root/tgsendmsg.sh "Good Night 晚安"
FT=`/usr/games/fortune | cconv -f UTF8-CN -t UTF8-TW`
FT=`echo "$FT" | sed -r "s/\x1B\[[0-9;]*[mK]//g"`
/root/tgsendmsg.sh "$FT"
fi
接下來就發揮想像力把能串接的掛上去, 也能多開幾隻 bot 送不同類型的訊息.
No comments:
Post a Comment