Skip to content

Commit

Permalink
Added support for getting user id via --server
Browse files Browse the repository at this point in the history
  • Loading branch information
sixlive committed Mar 3, 2024
1 parent c55ff6a commit bd8411c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 16 deletions.
28 changes: 12 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
# Telegrammer

Telegrammer is a command-line interface (CLI) tool written in Go that allows you to send messages or documents to a specified user on Telegram. It uses the Telegram Bot API to send messages and documents.
Telegrammer is a command-line interface (CLI) tool written in Go that allows you to send messages or documents to a specified user on Telegram, as well as retrieve your user ID for configuration by running in server mode. It uses the Telegram Bot API to send messages and documents.

## Features

- Send text messages to a specified user on Telegram.
- Send documents to a specified user on Telegram.
- Retrieve your user ID for configuration by running in server mode.
- Configurable through environment variables or a configuration file.
- Provides visual feedback on successful message delivery.

## Installation

To install Telegrammer, you need to have Go installed on your machine. Once you have Go set up, you can install Telegrammer with:

```
go get github.com/sixlive/telegrammer
```

or head over the the [releases](https://github.com/sixlive/telegrammer/releases) to download a pre-compiled binary.
or head over to the [releases](https://github.com/sixlive/telegrammer/releases) to download a pre-compiled binary.

## Usage

To use Telegrammer, you need to have a Telegram bot token and the user ID of the recipient. You can set these through environment variables or in a YAML configuration file.
To use Telegrammer, you need to have a Telegram bot token and the user ID of the recipient. You can set these through environment variables or in a YAML configuration file. Additionally, you can run Telegrammer in server mode to listen for new messages and find your user ID for configuration.

### Environment Variables

Expand All @@ -32,10 +31,7 @@ To use Telegrammer, you need to have a Telegram bot token and the user ID of the

You can also provide a configuration file in YAML format. The file should have the following structure:

```yaml
bot_key: your_bot_token
user_id: recipient_user_id
```
yaml bot_key: your_bot_token user_id: recipient_user_id

You can place the configuration file in the following locations:

Expand All @@ -45,20 +41,21 @@ You can place the configuration file in the following locations:
### Command-Line Flags

- `-f, --file` - Path to the file you want to send.
- `--server` - Run in server mode to listen for new messages and find your user ID.

### Examples

Send a text message:

```
telegrammer "Hello, world!"
```
`telegrammer "Hello, world!"`

Send a document:

```
telegrammer -f /path/to/document.pdf "Here's the document you requested."
```
`telegrammer -f /path/to/document.pdf "Here's the document you requested."`

Find your user ID by running in server mode:

`telegrammer --server`

## Contributing

Expand All @@ -67,4 +64,3 @@ Contributions to Telegrammer are welcome! Please submit a pull request or create
## License

Telegrammer is licensed under the MIT License. See [LICENSE](LICENSE) for more information.

66 changes: 66 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"

"github.com/charmbracelet/lipgloss"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
Expand All @@ -17,6 +20,7 @@ type AppConfig struct {
BotKey string `mapstructure:"bot_key"`
UserID int64 `mapstructure:"user_id"`
Filepath string // This will be set by a flag, not by viper directly.
Server bool // Flag to run in server mode
}

func initConfig() *AppConfig {
Expand Down Expand Up @@ -46,8 +50,14 @@ func main() {

// Setup flags and override config if flags are provided.
pflag.StringVarP(&config.Filepath, "file", "f", "", "Filepath to file")
pflag.BoolVar(&config.Server, "server", false, "Run in server mode to listen for new messages")
pflag.Parse()

if config.Server {
runServerMode(config)
return
}

messageText := pflag.Arg(0) // Get the first non-flag command-line argument.

bot, err := tgbotapi.NewBotAPI(config.BotKey)
Expand All @@ -62,6 +72,62 @@ func main() {
notifySuccess()
}

func runServerMode(config *AppConfig) {
bot, err := tgbotapi.NewBotAPI(config.BotKey)
if err != nil {
log.Fatalf("Failed to create bot: %v", err)
}

// Delete the existing webhook
_, err = bot.RemoveWebhook()
if err != nil {
log.Fatalf("Failed to remove webhook: %v", err)
}

// Wait a bit to ensure webhook is fully deleted
time.Sleep(time.Second * 1)

u := tgbotapi.NewUpdate(0)
u.Timeout = 60

updates, err := bot.GetUpdatesChan(u)
if err != nil {
log.Fatalf("Failed to get updates channel: %v", err)
}

for update := range updates {
if update.Message != nil {
displayDebugData(update)
os.Exit(0)
}
}
}

func displayDebugData(update tgbotapi.Update) {
// Check if the update contains a message
if update.Message == nil {
log.Println("Received update does not contain a message.")
return
}

// Marshal the message part of the update to JSON for readability
messageData, err := json.MarshalIndent(update.Message, "", " ")
if err != nil {
log.Printf("Error marshalling message data: %v", err)
return
}

// Create a Lipgloss style for the message data display
messageStyle := lipgloss.NewStyle().
Border(lipgloss.NormalBorder()).
Padding(1, 2).
Margin(1).
BorderForeground(lipgloss.Color("63"))

// Render the message data with the style and print it
fmt.Println(messageStyle.Render(string(messageData)))
}

func sendMessage(bot *tgbotapi.BotAPI, userID int64, messageText, filepath string) error {
if filepath != "" {
return sendDocument(bot, userID, messageText, filepath)
Expand Down
Binary file modified telegrammer
Binary file not shown.

0 comments on commit bd8411c

Please sign in to comment.