Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to works with Golang echo framework and Telegram bot?

I want to use "telegram bot" with "echo framework" (When the server started, echo and telegram bot work together). I used the below code, but when I ran that, the telegram bot didn't start.

My main.go:

package main

import (
    "database/sql"
    "log"
    "net/http"
    "strings"

    tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
    "github.com/labstack/echo"
    _ "github.com/mattn/go-sqlite3"
)

func main() {
    e := echo.New()
    e.GET("/", func(c echo.Context) error {
        return c.String(http.StatusOK, "Hello, World!")
    })
    _ = e.Start(":1323")

    db, err := sql.Open("sqlite3", "./criticism.db")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    bot, err := tgbotapi.NewBotAPI("")
    if err != nil {
        log.Fatal(err)
    }

    bot.Debug = true

    log.Printf("Authorized on account %s", bot.Self.UserName)

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

    updates, err := bot.GetUpdatesChan(u)

    for update := range updates {
        if update.Message == nil {
            continue
        }

        gp_msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Hi")
        bot.Send(gp_msg)
    }
}
like image 716
MohammadReza Avatar asked Oct 15 '22 08:10

MohammadReza


1 Answers

The problem is that when you start the echo server, then the code does not go any further.

In order to use both of them, you need to separate each of them into a different thread and also stop your program to finish and stop everything.

The simplest way is to separate the web server and telegram bot and start them separately:

func StartEcho() { 
    e := echo.New()
    e.GET("/", func(c echo.Context) error {
        return c.String(http.StatusOK, "Hello, World!")
    })
    _ = e.Start(":1323")
}

func StartBot() {
    bot, err := tgbotapi.NewBotAPI("")
    if err != nil {
        log.Fatal(err)
    }

    bot.Debug = true

    log.Printf("Authorized on account %s", bot.Self.UserName)

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

    updates, err := bot.GetUpdatesChan(u)

    for update := range updates {
        if update.Message == nil {
            continue
        }

        gp_msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Hi")
        bot.Send(gp_msg)
    }
}

And then call them:

func main() {
    # Start the echo server in a separate thread
    go StartEcho()

    db, err := sql.Open("sqlite3", "./criticism.db")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()
    
    # Start the bot in a separate thread
    go StartBot()

    # To stop the program to finish and close
    select{}
    
    # You can also use https://golang.org/pkg/sync/#WaitGroup instead.
}

Or you can just run the last one in the main thread:

func main() {
    # Start the echo server in a separate thread
    go StartEcho()

    db, err := sql.Open("sqlite3", "./criticism.db")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()
    
    # Start the bot
    StartBot()
}

But if the last one stops, so will your entire program and the echo server with it. So you have to recover any panics and don't allow it to stop.

like image 84
Navid Zarepak Avatar answered Nov 15 '22 04:11

Navid Zarepak