[add] /help /roll xDy
This commit is contained in:
commit
01b446e9f9
4
.dockerignore
Normal file
4
.dockerignore
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
node_modules
|
||||||
|
npm-debug.log
|
||||||
|
.env
|
||||||
|
.DS_Store
|
3
.env.local
Normal file
3
.env.local
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
BOT_TOKEN=
|
||||||
|
WEBHOOK_DOMAIN=
|
||||||
|
PORT=
|
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
/.env
|
||||||
|
/node_modules
|
21
Dockerfile
Normal file
21
Dockerfile
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
# sudo docker-compose up --build -d
|
||||||
|
# sudo docker build -t telegram-bot-api .
|
||||||
|
# sudo docker run -d --env-file .env --name telegram-bot-api telegram-bot-api
|
||||||
|
# sudo docker run -v /volume1/homes/JianMiau/www/certificate:/app/certificate -e TZ=Asia/Taipei --name=telegram-bot-api --restart always --net=host telegram-bot-api
|
||||||
|
# sudo docker-compose up -d --build
|
||||||
|
# 加上 --build 會幫你重建 image(如程式有改動)。
|
||||||
|
|
||||||
|
# 基礎 Node 映像
|
||||||
|
FROM node:19.4.0
|
||||||
|
|
||||||
|
# 設定工作目錄
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# 複製 package 檔案並安裝依賴
|
||||||
|
COPY package*.json ./
|
||||||
|
RUN npm install
|
||||||
|
|
||||||
|
# 複製所有程式碼
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
# 預設啟動指令由 docker-compose 指定
|
44
app.js
Normal file
44
app.js
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
// 關掉 webhook,專心用 polling 模式
|
||||||
|
// 執行以下指令取消 webhook(只需要做一次):
|
||||||
|
// curl -F "url=" https://api.telegram.org/bot7916405163:AAGllDyNHNRTPDJ4gjwFPDi-TFDyFwD4foA/setWebhook
|
||||||
|
|
||||||
|
|
||||||
|
require('dotenv').config(); // 加這行載入 .env 檔案
|
||||||
|
const TelegramBot = require('node-telegram-bot-api');
|
||||||
|
|
||||||
|
const token = process.env.BOT_TOKEN; // 從環境變數讀取 token
|
||||||
|
//括號裡面的內容需要改為在第5步獲得的Token
|
||||||
|
const bot = new TelegramBot(token, { polling: true });
|
||||||
|
//使用Long Polling的方式與Telegram伺服器建立連線
|
||||||
|
|
||||||
|
//收到Start訊息時會觸發這段程式
|
||||||
|
bot.onText(/\/start/, function (msg) {
|
||||||
|
const chatId = msg.chat.id; //用戶的ID
|
||||||
|
const resp = '你好'; //括號裡面的為回應內容,可以隨意更改
|
||||||
|
bot.sendMessage(chatId, resp); //發送訊息的function
|
||||||
|
});
|
||||||
|
|
||||||
|
bot.onText(/\/help/, function (msg) {
|
||||||
|
const chatId = msg.chat.id;
|
||||||
|
const resp = '才不告訴逆雷';
|
||||||
|
bot.sendMessage(chatId, resp);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 收到roll開頭的訊息時會觸發這段程式
|
||||||
|
bot.onText(/\/roll(?:@[\w_]+)?(?:\s+(\d*)d(\d+))?/i, function (msg, match) {
|
||||||
|
// console.log(`✅ Bot roll`);
|
||||||
|
|
||||||
|
const chatId = msg.chat.id;
|
||||||
|
let count = parseInt(match[1]) || 1;
|
||||||
|
let sides = parseInt(match[2]) || 6;
|
||||||
|
|
||||||
|
if (count > 100 || sides > 1000) {
|
||||||
|
return bot.sendMessage(chatId, '⚠️ 數字過大,請保持理性!');
|
||||||
|
}
|
||||||
|
|
||||||
|
const rolls = Array.from({ length: count }, () => Math.floor(Math.random() * sides) + 1);
|
||||||
|
const total = rolls.reduce((sum, v) => sum + v, 0);
|
||||||
|
|
||||||
|
const resp = `🎲 ${count}d${sides} 擲出:\n🎯 ${rolls.join(' + ')} = ${total}`;
|
||||||
|
bot.sendMessage(chatId, resp);
|
||||||
|
});
|
13
docker-compose.yml
Normal file
13
docker-compose.yml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
version: '3.8'
|
||||||
|
|
||||||
|
services:
|
||||||
|
telegram-bot:
|
||||||
|
build: .
|
||||||
|
container_name: telegram-bot
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
|
restart: unless-stopped
|
||||||
|
volumes:
|
||||||
|
- .:/app
|
||||||
|
working_dir: /app
|
||||||
|
command: node app.js
|
2185
package-lock.json
generated
Normal file
2185
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
15
package.json
Normal file
15
package.json
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"dependencies": {
|
||||||
|
"dotenv": "^16.5.0",
|
||||||
|
"node-telegram-bot-api": "^0.66.0"
|
||||||
|
},
|
||||||
|
"name": "telegram-bot-api",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"main": "app.js",
|
||||||
|
"scripts": {
|
||||||
|
"start": "node app.js"
|
||||||
|
},
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"description": ""
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user