Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/raminsa/telegram_api

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/raminsa/telegram_api

  • v0.7.7
  • Source
  • Go
  • Socket score

Version published
Created
Source

Telegram Bot API

The Telegram Bot API provides an HTTP API for creating Telegram Bots.

If you've got any questions about bots or would like to report an issue with your bot, kindly contact us at @BotSupport in Telegram.

Please note that only global Bot API issues that affect all bots are suitable for this repository.

To learn how to use it, please see our examples.

Bot API 7.7 recent changes July 7, 2024.

Table of Contents

Installation

go get github.com/raminsa/telegram-bot-api.

Documentation

See Bots: An introduction for developers for a brief description of Telegram Bots and their features.

See the Telegram Bot API documentation for a description of the Bot API interface and a complete list of available classes, methods and updates.

See the Telegram Bot API server build instruction generator for detailed instructions on how to build the Telegram Bot API server.

Subscribe to @BotNews to be the first to know about the latest updates and join the discussion in @BotTalk.

Example

use get update method (simple):

package main

import (
	"fmt"
	"log"

	"github.com/raminsa/telegram-bot-api/telegram"
)

func main() {
	tg, err := telegram.New("BotToken")
	if err != nil {
		log.Fatal(err)
	}

	getUpdates := tg.NewGetUpdates()
	getUpdates.Offset = 1
	getUpdates.Timeout = 60
	getUpdates.Limit = 100
	updates := tg.GetUpdatesChan(getUpdates)
	for update := range updates {
		fmt.Println(update.UpdateID, getUpdates.Offset)
		if update.UpdateID >= getUpdates.Offset {
			getUpdates.Offset = update.UpdateID + 1
		}
		if update.Message != nil {
			fmt.Println(update.Message.Text)
		}
	}
}

use webhook method (http):

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/raminsa/telegram-bot-api/telegram"
)

func main() {
	fmt.Println("start at port:", "BotPortNumber")
	err := http.ListenAndServe("BotPortNumber", http.HandlerFunc(handleWebhook))
	if err != nil {
		log.Fatal(err)
	}
}

func handleWebhook(w http.ResponseWriter, r *http.Request) {
	update, err := telegram.HandleUpdate(r)
	if err != nil {
		err = telegram.HandleUpdateError(w, err)
		if err != nil {
			//handle err
		}
		return
	}

	if update.Message != nil {
		fmt.Println(update.Message.Text)
	}
}

use webhook method (https):

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/raminsa/telegram-bot-api/telegram"
)

func main() {
	fmt.Println("start at port:", "BotPortNumber")
	err := http.ListenAndServeTLS("BotPortNumber", "BotCertFile", "BotKeyFile", http.HandlerFunc(handleWebhook))
	if err != nil {
		log.Fatal(err)
	}
}

func handleWebhook(w http.ResponseWriter, r *http.Request) {
	update, err := telegram.HandleUpdate(r)
	if err != nil {
		err := telegram.HandleUpdateError(w, err)
		if err != nil {
			//handle err
		}
		return
	}

	if update.Message != nil {
		fmt.Println(update.Message.Text)
	}
}

to generate your cert file use this. See self-signed guide for details.:

openssl req -newkey rsa:2048 -sha256 -nodes -keyout <file.key> -x509 -days 36500 -out <file.pem> -subj "/C=US/ST=New York/L=Brooklyn/O=Example Brooklyn Company/CN=<server_address>"

use a channel to handle all requests (Avoid ReadTimeoutExpired error), http.ListenAndServe() supports concurrency:

package main

import (
	"fmt"
	"net/http"

	"github.com/raminsa/telegram-bot-api/telegram"
	"github.com/raminsa/telegram-bot-api/types"
)

func main() {
	fmt.Println("start at port:", "BotPortNumber")
	updates := listenForWebhook(100)
	go http.ListenAndServeTLS("BotPortNumber", "BotCertFile", "BotKeyFile", nil)
	for update := range updates {
		if update.Message != nil {
			fmt.Println(update.Message.Text)
		}
	}
}

func listenForWebhook(maxWebhookConnections int) types.UpdatesChannel {
	ch := make(chan types.Update, maxWebhookConnections)
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		if update, err := telegram.HandleUpdate(r); err != nil {
			return
		} else {
			ch <- *update
		}
	})

	return ch
}

Custom Client

use client with custom options:

package main

import (
	"fmt"
	"log"

	"github.com/raminsa/telegram-bot-api/telegram"
)

func main() {
	client := telegram.Client()
	client.BaseUrl = "baseUrl"
	client.Proxy = "proxy"
	client.ForceV4 = true
	client.DisableSSLVerify = true
	client.ForceAttemptHTTP2 = true
	tg, err := telegram.NewWithCustomClient("BotToken", client)
	if err != nil {
		log.Fatal(err)
	}

	me, err := tg.GetMe()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("botID:", me.ID, "botUsername:", me.UserName)
}

Debug

use debug option and write to local file:

package main

import (
	"fmt"
	"log"

	"github.com/raminsa/telegram-bot-api/telegram"
)

func main() {
	tg, err := telegram.NewWithBaseUrl("BotToken", "baseUrl")
	if err != nil {
		log.Fatal(err)
	}

	//active debug mode
	tg.Bot.Debug = true

	me, err := tg.GetMe()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("botID:", me.ID, "botUsername:", me.UserName)

	//access debug log
	//method 1: write to console
	fmt.Println(tg.GetLoggerFile())

	//method 2: write to file
	err = tg.WriteLoggerFile("fileName")
	if err != nil {
		log.Fatal(err)
	}
}

FAQs

Package last updated on 07 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc