Skip to content
This repository has been archived by the owner on Jul 20, 2024. It is now read-only.

Commit

Permalink
return to use node-telgram-bot-api nodejs template, add new assets
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed May 7, 2022
1 parent 2d26f64 commit 63467fa
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 38 deletions.
10 changes: 6 additions & 4 deletions internal/pipes/new/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,16 @@ func buildBot(msg tea.Msg, m model, botName string) (tea.Model, tea.Cmd) {
viper.SetConfigType("yaml")

viper.SetDefault("author", conf.String("user.github_username"))
viper.SetDefault("bot.name", opts.BotName)
viper.SetDefault("bot.version", "0.1.0")
viper.SetDefault("bot.type", BotType(m))
viper.SetDefault("bot.lang", BotLang(m))
viper.SetDefault("bot.name", opts.BotName)
viper.SetDefault("bot.package_manager", BotPM(m))
viper.SetDefault("bot.type", BotType(m))
viper.SetDefault("bot.start_cmd", BotStartCmd(m))
viper.SetDefault("bot.version", "0.1.0")
dockerImage := conf.String("user.docker_id") + "/" + opts.BotName
viper.SetDefault("docker.image", dockerImage)
viper.SetDefault("docker.build_cmd", "docker build -t " + dockerImage + " # you can edit the build command")
viper.SetDefault("docker.build_cmd", "docker build -t " + dockerImage + " .")
viper.SetDefault("docker.run_cmd", "docker run -it " + dockerImage)

if err := viper.SafeWriteConfig(); err != nil {
if os.IsNotExist(err) {
Expand Down
29 changes: 29 additions & 0 deletions internal/pipes/new/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package new
import (
"fmt"
"os"
"runtime"
"strings"

"github.com/abdfnx/botway/constants"
Expand Down Expand Up @@ -58,6 +59,34 @@ func BotLang(m model) string {
return ""
}

func BotStartCmd(m model) string {
if m.LangChoice == 0 {
if runtime.GOOS == "windows" {
return `py .\src\main.py`
} else {
return `python3 ./src/main.py`
}
} else if m.LangChoice == 0 && m.PMCoice == 1 {
if runtime.GOOS == "windows" {
return `pipenv run py .\src\main.py`
} else {
return `pipenv run python3 ./src/main.py`
}
} else if m.LangChoice == 1 {
return "go run src/main.go"
} else if m.LangChoice == 2 {
return "node src/main.js"
} else if m.LangChoice == 3 {
return "bundle exec ruby src/main.rb"
} else if m.LangChoice == 4 {
return "cargo run src/main.rs"
} else if m.LangChoice == 5 {
return "deno run --allow-all src/main.ts"
}

return ""
}

func BotPM(m model) string {
if m.LangChoice == 0 && m.PMCoice == 0 {
return "pip"
Expand Down
2 changes: 2 additions & 0 deletions templates/telegram/go/assets/src/main.go.bw
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"log"
"time"

Expand All @@ -25,5 +26,6 @@ func main() {
return c.Send("Hello!")
})

fmt.Println("Bot started")
b.Start()
}
Binary file added templates/telegram/nodejs/assets/src/bot.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
100 changes: 72 additions & 28 deletions templates/telegram/nodejs/assets/src/index.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,87 @@
import { Telegraf } from "telegraf";
const { GetToken } = require("botway.js");
const TelegramBot = require("node-telegram-bot-api");
const botway = require("botway.js");
const request = require("request");

const bot = new Telegraf(GetToken());
const options = {
polling: true,
};

bot.command("quit", (ctx) => {
// Explicit usage
ctx.telegram.leaveChat(ctx.message.chat.id);
const bot = new TelegramBot(botway.GetToken(), options);

// Using context shortcut
ctx.leaveChat();
// Matches /photo
bot.onText(/\/photo/, function onPhotoText(msg) {
// From file path
const photo = `${__dirname}/bot.gif`;

bot.sendPhoto(msg.chat.id, photo, {
caption: "I'm a bot!",
});
});

// Matches /crypto
bot.onText(/\/crypto/, function onFavoriteCryptoText(msg) {
const opts = {
reply_to_message_id: msg.message_id,
reply_markup: JSON.stringify({
keyboard: [
["BTC", "ETH", "LTC"],
["EOS", "XRP", "SOL"],
],
}),
};
bot.sendMessage(msg.chat.id, "What is your favorite crypto currency?", opts);
});

bot.on("text", (ctx) => {
// Explicit usage
ctx.telegram.sendMessage(ctx.message.chat.id, `Hello ${ctx.state.role}`);
// Matches /audio
bot.onText(/\/audio/, function onAudioText(msg) {
// From HTTP request
const url = "https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg";
const audio = request(url);

// Using context shortcut
ctx.reply(`Hello ${ctx.state.role}`);
bot.sendAudio(msg.chat.id, audio);
});

bot.on("callback_query", (ctx) => {
// Explicit usage
ctx.telegram.answerCbQuery(ctx.callbackQuery.id);
// Matches /echo [whatever]
bot.onText(/\/echo (.+)/, function onEchoText(msg, match) {
const resp = match[1];

// Using context shortcut
ctx.answerCbQuery();
bot.sendMessage(msg.chat.id, resp);
});

bot.on("inline_query", (ctx) => {
const result = [];
// Explicit usage
ctx.telegram.answerInlineQuery(ctx.inlineQuery.id, result);
// Matches /editable
bot.onText(/\/editable/, function onEditableText(msg) {
const opts = {
reply_markup: {
inline_keyboard: [
[
{
text: "Edit Text",
// we shall check for this value when we listen
// for "callback_query"
callback_data: "edit",
},
],
],
},
};

// Using context shortcut
ctx.answerInlineQuery(result);
bot.sendMessage(msg.from.id, "Original Text", opts);
});

bot.launch();
// Handle callback queries
bot.on("callback_query", function onCallbackQuery(callbackQuery) {
const action = callbackQuery.data;
const msg = callbackQuery.message;
const opts = {
chat_id: msg.chat.id,
message_id: msg.message_id,
};

// Enable graceful stop
process.once("SIGINT", () => bot.stop("SIGINT"));
process.once("SIGTERM", () => bot.stop("SIGTERM"));
let text;

if (action === "edit") {
text = "Edited Text";
}

bot.editMessageText(text, opts);
});
5 changes: 5 additions & 0 deletions templates/telegram/nodejs/npm/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func TelegramNodejsNpm(botName string) {
dockerFile := os.WriteFile(filepath.Join(botName, "Dockerfile"), []byte(DockerfileContent(botName)), 0644)
procFile := os.WriteFile(filepath.Join(botName, "Procfile"), []byte("process: node ./src/index.js"), 0644)
resourcesFile := os.WriteFile(filepath.Join(botName, "resources.md"), []byte(nodejs.Resources()), 0644)
botGifFile := os.WriteFile(filepath.Join(botName, "src", "bot.gif"), []byte(nodejs.BotGif()), 0644)

if botGifFile != nil {
log.Fatal(botGifFile)
}

if resourcesFile != nil {
log.Fatal(resourcesFile)
Expand Down
5 changes: 5 additions & 0 deletions templates/telegram/nodejs/pnpm/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func TelegramNodejsPnpm(botName string) {
dockerFile := os.WriteFile(filepath.Join(botName, "Dockerfile"), []byte(DockerfileContent(botName)), 0644)
procFile := os.WriteFile(filepath.Join(botName, "Procfile"), []byte("process: node ./src/index.js"), 0644)
resourcesFile := os.WriteFile(filepath.Join(botName, "resources.md"), []byte(nodejs.Resources()), 0644)
botGifFile := os.WriteFile(filepath.Join(botName, "src", "bot.gif"), []byte(nodejs.BotGif()), 0644)

if botGifFile != nil {
log.Fatal(botGifFile)
}

if resourcesFile != nil {
log.Fatal(resourcesFile)
Expand Down
19 changes: 13 additions & 6 deletions templates/telegram/nodejs/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package nodejs

import "github.com/abdfnx/botway/templates"

var Packages = "telegraf botway.js"
var Packages = "node-telegram-bot-api botway.js"

func IndexJSContent() string {
return templates.Content("telegram", "nodejs", "src/index.js", "")
}

func BotGif() string {
return templates.Content("telegram", "nodejs", "src/bot.gif", "")
}

func Resources() string {
return `# Botway Telegram (Node.js) Resources
Expand All @@ -19,13 +23,16 @@ func Resources() string {
## API
- [Modern Telegram Bot Framework for Node.js](https://github.com/telegraf/telegraf)
- [Telegraf Docs](https://github.com/telegraf/telegraf/tree/v4/docs)
- [Telegraf Telegram Channel](https://t.me/TelegrafJSChat)
- [Telegram Bot API for NodeJS](https://github.com/https://github.com/yagop/node-telegram-bot-api)
- [node-telegram-bot-api Docs](https://github.com/yagop/node-telegram-bot-api/tree/master/doc)
- [node-telegram-bot-api Help Information](https://github.com/yagop/node-telegram-bot-api/blob/master/doc/help.md)
- [Tutorials](https://github.com/yagop/node-telegram-bot-api/tree/master/doc/tutorials.md)
- [node-telegram-bot-api Telegram Channel](https://t.me/node_telegram_bot_api)
- [node-telegram-bot-api Telegram Group](https://t.me/ntbasupport)
## Examples
[Examples](https://github.com/telegraf/telegraf/tree/v4/docs/examples)
[Examples](https://github.com/yagop/node-telegram-bot-api/tree/master/examples)
big thanks to [**@telegraf**](https://github.com/telegraf/telegraf) org`
big thanks to [**@yagop**](https://github.com/yagop)`
}
5 changes: 5 additions & 0 deletions templates/telegram/nodejs/yarn/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func TelegramNodejsYarn(botName string) {
dockerFile := os.WriteFile(filepath.Join(botName, "Dockerfile"), []byte(DockerfileContent(botName)), 0644)
procFile := os.WriteFile(filepath.Join(botName, "Procfile"), []byte("process: node ./src/index.js"), 0644)
resourcesFile := os.WriteFile(filepath.Join(botName, "resources.md"), []byte(nodejs.Resources()), 0644)
botGifFile := os.WriteFile(filepath.Join(botName, "src", "bot.gif"), []byte(nodejs.BotGif()), 0644)

if botGifFile != nil {
log.Fatal(botGifFile)
}

if resourcesFile != nil {
log.Fatal(resourcesFile)
Expand Down

0 comments on commit 63467fa

Please sign in to comment.