整一個Telegram Bot for 譚仔食咩好

每次去譚仔都要諗食咩好
所以就有左呢個bot

今次用既係node.js 同埋library telegraf

第一步開左project folder先

mkdir moomin-bot
cd moomin-bot
code .

然後用@BotFather 開一個bot出黎
newbot.png 打/newbot

之後開完可以去API Token 拎返黎放入.env度   moominbot.png

BOT_TOKEN=YOUR_TOKEN_HERE

輸入完就可以開一個main.js
入面輸入

const { Telegraf } = require('telegraf')
const bot = new Telegraf(process.env.BOT_TOKEN)
bot.start((ctx) => ctx.reply('Welcome'))
bot.help((ctx) => ctx.reply('Send me a sticker'))
bot.on('sticker', (ctx) => ctx.reply('👍'))
bot.hears('hi', (ctx) => ctx.reply('Hey there'))
bot.launch()

command line 度打node main 如無意外而家入去個bot度就會見到以下畫面 reply.png 去到呢步已經成功左大半

之後就係寫譚仔function
define 個menu

  const dishes = [
    "酸菜",
    "腐皮",
    "魚蛋",
    "火腿",
    "豆卜",
    "金菇",
    "韭菜",
    "芽菜",
    "生菜",
    "魚腐",
    "牛肉",
    "豬膶",
    "豬肉",
    "牛丸",
    "雞肉",
    "魚片頭",
    "炸醬",
    "鮮冬菇",
    "墨魚春卷",
    "腩肉",
    "豬大腸",
    "墨丸",
    "竹笙",
    "黑柏葉",
    "芫茜山",
    "皮蛋",
    "脆肉鯇",
  ];

當然仲有小食,湯底,幾辣
不過一樣寫法,呢度都唔多寫
然後我既做法係shuffle 個array
之後隨機餸同小食抽N樣野出黎
最尾好土炮地format 個output
gettamjai.png

最後加一行

bot.command("tamjai", (ctx) => {
  const tamjai = getTamJai();
  return ctx.replyWithHTML(tamjai);
 }
);

當對住個bot輸入tamjai 既時候就會自動random 食咩出黎啦
result.png

重口味之選黎

完成之後就將佢docker化然後push上aws server
咁就完成啦個tg bot

Copyright © tnlo.me. All rights reserved.