Cookie Consent by Free Privacy Policy Generator ๐Ÿ“Œ How to build a Telegram Bot that Send Quote after every 10 Send

๐Ÿ  Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeitrรคge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden รœberblick รผber die wichtigsten Aspekte der IT-Sicherheit in einer sich stรคndig verรคndernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch รผbersetzen, erst Englisch auswรคhlen dann wieder Deutsch!

Google Android Playstore Download Button fรผr Team IT Security



๐Ÿ“š How to build a Telegram Bot that Send Quote after every 10 Send


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

In this Tutorial we will build a Telegram Bot that fetch Quote from a Website and send it to Telegram.

For Basic Knowledge and How BOT is build inside telegram: Follow these Instructions

https://core.telegram.org/bots/tutorial

Imports

import (
    "encoding/json"
    "log"
    "net/http"
    "strconv"
    "time"

    "github.com/go-co-op/gocron"
    "github.com/hashicorp/go-hclog"
  tgBOT "github.com/go-telegram-bot-api/telegram-bot-api/v5"


)

Step -1

First we will fetch a quote from a URL

https://zenquotes.io/api/random

This Website give us JSON response of Some Random Quotes You can See it in your Browser.
Than based on JSON Objects we will get from this website we will build our Data Model

type RandomQuotes struct {
    Quote  string `json:"q"`
    Author string `json:"a"`
}

This JSON model will provide us Quote as well as Author same as the JSON Object we will get from our URL

func FetchQuote() string {
    // fetch the result from the website
    resp, err := http.Get("https://zenquotes.io/api/random")
    // if we end up any errors for http errors - e.g if we hit the url too many times
    if err != nil {
        log.Fatalln(err)
    }

    // than we will close the response - defer means this line of code will execute at the end of function
    defer resp.Body.Close()

    // storage for quote we will get
    var quotes []RandomQuotes

    // than we will decode the JSON Object BODY Response
    json.NewDecoder(resp.Body).Decode(&quotes)

    // if we get response like for example if we get QUOTE Response
    // we will return it as TEXT Object
    if len(quotes) > 0 {
        println(quotes[0].Quote + " - " + quotes[0].Author)
        return quotes[0].Quote + " - " + quotes[0].Author
    }
    // Otherwise Empty String
    return ""
}

Step -2

func QuoteURL(message string) {
    // Here we will directly Call the TELEGRAM sendMessage  API that means we don't hve to use any warper
    var URL string = "https://api.telegram.org/bot" + apiTOKEN + "/sendMessage?chat_id=" + strconv.Itoa(chatID) + "&text=" + message
    // is the message we will enter if it's not empty
    if message != "" {
        // we will get the JSON response
        resp, err := http.Get(URL)
        if err != nil {
            log.Fatalln(err)
        }
        // Than we will close the response
        defer resp.Body.Close()
    }
}

Step -3

GoCorn is a Library that will call the api after every 10 Send and send response to our Telegram

var apiTOKEN string = "123456789:AAE8zD_pYDH8DkDKeaOYUIPKIfczHGHGSHEI"

var chatID int = 123244576774

func QuoteBOT() {

    logger := NewLogger()
    s := gocron.NewScheduler(time.UTC)
    // Reset limit send mail for users.
    _, err := s.Every(10).Seconds().Do(func() {
        logger.Info("calling URL....")
        // get message from getQuote function
        message := FetchQuote()
        QuoteURL(message)
    })

    if err != nil {
        logger.Error("Error scheduling limit data", "error", err)
        return
    }

    // s.StartAsync()
    s.StartBlocking()
}

Bonus

If you wanna use TELEGRAM Wrapper for API calling Skip the
Step 3

go get "github.com/go-telegram-bot-api/telegram-bot-api/v5"
        updates := bot.GetUpdatesChan(updateConfig)
        for update := range updates {
            if update.Message == nil {
                continue
            }
            data, dataErr := FetchQuote()
            if dataErr != nil {
                log.Fatal(dataErr)
            }
            println(data)
            message := tgBOT.NewMessage(update.Message.Chat.ID, data)

            bot.Send(message)

        }

Basic Loging

// NewLogger returns a new logger instance
func NewLogger() hclog.Logger {
    logger := hclog.New(&hclog.LoggerOptions{
        Name:  "telegram-bot-service",
        Level: hclog.LevelFromString("DEBUG"),
    })

    return logger
}

Final Thoughts:

Building a Basic BOT is really simple but based on use case it can be very complex and advance. For more advance cases I will post some more content. Like How to implement Trading BOT, Finance BOT etc.

...



๐Ÿ“Œ How to build a Telegram Bot that Send Quote after every 10 Send


๐Ÿ“ˆ 76.2 Punkte

๐Ÿ“Œ Khamil Landross and Zack Jones EFTP 2.0.7.337 Command LIST/QUOTE SIZE/QUOTE MDTM directory traversal


๐Ÿ“ˆ 35.6 Punkte

๐Ÿ“Œ Telegram C# C2 - A Command and Control Tool for Telegram Bot Communication


๐Ÿ“ˆ 29.31 Punkte

๐Ÿ“Œ How to Build a Telegram Bot using Typescript & Node.js


๐Ÿ“ˆ 25.42 Punkte

๐Ÿ“Œ How to Build and Deploy a python-telegram-bot v20 Webhook


๐Ÿ“ˆ 25.42 Punkte

๐Ÿ“Œ AI enthusiasm #7 - Build an AI-powered Telegram Bot!๐Ÿค–


๐Ÿ“ˆ 25.42 Punkte

๐Ÿ“Œ fortune-mod release 3.4.0: with some new quotations and a more robust build process (FOSS CLI utility for displaying a random quote)


๐Ÿ“ˆ 23.21 Punkte

๐Ÿ“Œ Build a Random Quote Generator with HTML, CSS, and JavaScript


๐Ÿ“ˆ 23.21 Punkte

๐Ÿ“Œ Build a Philosophy Quote Generator With Vector Search and Astra DB (Part 2)


๐Ÿ“ˆ 23.21 Punkte

๐Ÿ“Œ Build a Philosophy Quote Generator With Vector Search and Astra DB


๐Ÿ“ˆ 23.21 Punkte

๐Ÿ“Œ Build a Philosophy Quote Generator With Vector Search and Astra DB (Part 3)


๐Ÿ“ˆ 23.21 Punkte

๐Ÿ“Œ Wikipediaโ€™s bot-on-bot battles that can last for years


๐Ÿ“ˆ 21.43 Punkte

๐Ÿ“Œ Accelerate bot development with Bot Framework SDK and other updates


๐Ÿ“ˆ 21.43 Punkte

๐Ÿ“Œ Bot Framework โ€“ Using custom adapters to surface your bot anywhere (Part 3 of 3)


๐Ÿ“ˆ 21.43 Punkte

๐Ÿ“Œ Bot Framework Composer: Bot Frameworkโ€™s new collaborative Conversational AI development environment


๐Ÿ“ˆ 21.43 Punkte

๐Ÿ“Œ 2021 Bot Management Research: Besorgnis gegenรผber raffinierten Bot-Angriffen nimmt zu


๐Ÿ“ˆ 21.43 Punkte

๐Ÿ“Œ Android GM Bot malware bot source code leaked online


๐Ÿ“ˆ 21.43 Punkte

๐Ÿ“Œ Android GM Bot malware bot source code leaked online


๐Ÿ“ˆ 21.43 Punkte

๐Ÿ“Œ Testing and deploying a weather bot with Python + Azure Bot Service


๐Ÿ“ˆ 21.43 Punkte

๐Ÿ“Œ 2022 State of Bot Mitigation Report: Growing Majority of Companies (69%) Lose Revenue Due to Bot-Driven Account Fraud


๐Ÿ“ˆ 21.43 Punkte

๐Ÿ“Œ DataDome's Inaugural E-Commerce Holiday Bot & Online Fraud Report Reveals the U.S. as the Top Source of Bot Attacks


๐Ÿ“ˆ 21.43 Punkte

๐Ÿ“Œ Gerรผchte um ChatGPT: Bau mir einen Bot, Bot!


๐Ÿ“ˆ 21.43 Punkte

๐Ÿ“Œ Study Reveals Bot-On-Bot Editing Wars Raging On Wikipedia's Pages


๐Ÿ“ˆ 21.43 Punkte

๐Ÿ“Œ Bot vs Bot in Never-Ending Cycle of Improving Artificial intelligence


๐Ÿ“ˆ 21.43 Punkte

๐Ÿ“Œ A Reporter Built a Bot To Find Nazi Sock Puppet Accounts. Twitter Banned the Bot and Kept the Nazis


๐Ÿ“ˆ 21.43 Punkte

๐Ÿ“Œ Bot Framework โ€“ Using custom adapters to surface your bot anywhere (Part 3 of 3) | AI Show


๐Ÿ“ˆ 21.43 Punkte

๐Ÿ“Œ 2021 Bot Management Research: Besorgnis gegenรผber raffinierten Bot-Angriffen nimmt zu


๐Ÿ“ˆ 21.43 Punkte

๐Ÿ“Œ Netacea unveils bot management framework to combat malicious bot attacks


๐Ÿ“ˆ 21.43 Punkte

๐Ÿ“Œ 2022 State of Bot Mitigation Report: Growing Majority of Companies (69%) Lose Revenue Due to Bot-Driven Account Fraud


๐Ÿ“ˆ 21.43 Punkte

๐Ÿ“Œ F5 Distributed Cloud Bot Defense protects customers against bot attacks on AWS


๐Ÿ“ˆ 21.43 Punkte

๐Ÿ“Œ Edgio Advanced Bot Management protects users against bot attacks


๐Ÿ“ˆ 21.43 Punkte

๐Ÿ“Œ Telegram startet neue Bot-Plattform 2.0


๐Ÿ“ˆ 20.01 Punkte

๐Ÿ“Œ Telegram startet Fรถrderung von Bot-Entwicklern


๐Ÿ“ˆ 20.01 Punkte











matomo