All Projects → gotd → Td

gotd / Td

Licence: mit
Pure Go Telegram MTProto API Client

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Td

Unigram
A Telegram desktop app made for Windows 10
Stars: ✭ 1,393 (+1080.51%)
Mutual labels:  telegram, mtproto
Teleperl
Pure perl mtproto/telergam client
Stars: ✭ 34 (-71.19%)
Mutual labels:  telegram, mtproto
Chatengine
Open source mtproto server written in golang with compatible telegram client
Stars: ✭ 544 (+361.02%)
Mutual labels:  telegram, mtproto
Telethon
Pure Python 3 MTProto API Telegram client library, for bots too!
Stars: ✭ 5,805 (+4819.49%)
Mutual labels:  telegram, mtproto
Callsmusic
The first open-source PyTgCalls-based project.
Stars: ✭ 62 (-47.46%)
Mutual labels:  telegram, mtproto
Telegram Mtproto
Telegram client api (MTProto) library
Stars: ✭ 542 (+359.32%)
Mutual labels:  telegram, mtproto
Telegramd
Unofficial open source telegram server written in golang
Stars: ✭ 609 (+416.1%)
Mutual labels:  telegram, mtproto
WilliamButcherBot
Telegram Group Manager Bot Written In Python Using Pyrogram.
Stars: ✭ 187 (+58.47%)
Mutual labels:  telegram, mtproto
Grammers
(tele)gramme.rs - use Telegram's API from Rust
Stars: ✭ 109 (-7.63%)
Mutual labels:  telegram, mtproto
Mtprotoproxy
Async MTProto proxy for Telegram
Stars: ✭ 1,014 (+759.32%)
Mutual labels:  telegram, mtproto
Mtproxy
MTProxyTLS一键安装绿色脚本
Stars: ✭ 256 (+116.95%)
Mutual labels:  telegram, mtproto
Telegram React
Experimental Telegram web client with tdlib, webassembly and react js under the hood
Stars: ✭ 1,332 (+1028.81%)
Mutual labels:  telegram, mtproto
mtproxy autoinstaller
MTProxy autoinstaller for Ubuntu
Stars: ✭ 25 (-78.81%)
Mutual labels:  telegram, mtproto
Novagram
An Object-Oriented PHP library for Telegram Bots
Stars: ✭ 112 (-5.08%)
Mutual labels:  telegram, mtproto
Telega
C# Telegram MTProto Client
Stars: ✭ 38 (-67.8%)
Mutual labels:  telegram, mtproto
Mtproto
Full-native go implementation of Telegram API
Stars: ✭ 566 (+379.66%)
Mutual labels:  telegram, mtproto
Jsmtproxy
High Performance NodeJS MTProto Proxy
Stars: ✭ 254 (+115.25%)
Mutual labels:  telegram, mtproto
node-tg-native
Telegram Native lib
Stars: ✭ 22 (-81.36%)
Mutual labels:  telegram, mtproto
Mtg
Bullshit-free MTPROTO proxy for Telegram
Stars: ✭ 976 (+727.12%)
Mutual labels:  telegram, mtproto
Mtproto
Golang MTProto implementation. Current API layer is 65
Stars: ✭ 86 (-27.12%)
Mutual labels:  telegram, mtproto

td Go Reference codecov

Fast Telegram client fully in go.

Before using this library, read How Not To Get Banned guide.

Status

Work is still in progress (mostly helpers and convenience wrappers), but basic functionality were tested in production and works fine. Only go1.16 is supported and no backward compatibility is provided for now.

Goal of this project is to implement stable, performant and safe client for Telegram in go while providing simple and convenient API, including feature parity with TDLib.

This project is fully non-commercial and not affiliated with any commercial organization (including Telegram LLC).

Features

  • Full MTProto 2.0 implementation in go
  • Code for Telegram types generated by ./cmd/gotdgen (based on gotd/tl parser) with embedded official documentation
  • Pluggable session storage
  • Automatic re-connects with keepalive
  • Vendored Telegram public keys that are kept up-to-date
  • Rigorously tested
    • End-to-end with real Telegram server in CI
    • End-to-end with gotd Telegram server (in go)
    • Lots of unit testing
    • Fuzzing
    • 24/7 canary bot in production that tests reconnects, update handling, memory leaks and performance
  • No runtime reflection overhead
  • Conforms to Security guidelines for Telegram client software developers
    • Secure PRNG used for crypto
    • Replay attack protection
  • 2FA support
  • MTProxy support
  • Multiple helpers that hide complexity of raw Telegram API
    • uploads for big and small files with multiple streams for single file and progress reporting
    • downloads with CDN support, also multiple streams
    • messages with various convenience builders and text styling support
    • query with pagination helpers
  • CDN support with connection pooling
  • Automatic datacenter migration and redirects handling
  • Graceful request cancellation via context

Example

You can see cmd/gotdecho for simple echo bot example or gotd/bot that can recover from restarts and fetch missed updates (and also is used as canary for stability testing).

Auth

User

You can use td/telegram/AuthFlow to simplify user authentication flow.

codePrompt := func(ctx context.Context) (string, error) {
    // NB: Use "golang.org/x/crypto/ssh/terminal" to prompt password.
    fmt.Print("Enter code: ")
    code, err := bufio.NewReader(os.Stdin).ReadString('\n')
    if err != nil {
        return "", err
    }
    return strings.TrimSpace(code), nil
}
// This will setup and perform authentication flow.
// If account does not require 2FA password, use telegram.CodeOnlyAuth
// instead of telegram.ConstantAuth.
if err := telegram.NewAuth(
    telegram.ConstantAuth(phone, password, telegram.CodeAuthenticatorFunc(codePrompt)),
    telegram.SendCodeOptions{},
).Run(ctx, client); err != nil {
    panic(err)
}

Bot

Use bot token from @BotFather.

if err := client.AuthBot(ctx, "token:12345"); err != nil {
    panic(err)
}

Calling MTProto directly

You can use generated tg.Client that allows calling any MTProto methods directly.

// Grab these from https://my.telegram.org/apps.
// Never share it or hardcode!
client := telegram.NewClient(appID, appHash, telegram.Options{})
client.Run(ctx, func(ctx context.Context) error) {
  // Grab token from @BotFather.
  if err := client.AuthBot(ctx, "token:12345"); err != nil {
    return err
  }
  state, err := tg.NewClient(client).UpdatesGetState(ctx)
  if err != nil {
    return err
  }
  // Got state: &{Pts:197 Qts:0 Date:1606855030 Seq:1 UnreadCount:106}
  // This will close client and cleanup resources.
  return nil
}

Generated code

Code output of gotdgen contains references to TL types, examples, URL to official documentation and extracted comments from it.

For example, the auth.Authorization type in tg/tl_auth_authorization_gen.go:

// AuthAuthorizationClass represents auth.Authorization generic type.
//
// See https://core.telegram.org/type/auth.Authorization for reference.
//
// Example:
//  g, err := DecodeAuthAuthorization(buf)
//  if err != nil {
//      panic(err)
//  }
//  switch v := g.(type) {
//  case *AuthAuthorization: // auth.authorization#cd050916
//  case *AuthAuthorizationSignUpRequired: // auth.authorizationSignUpRequired#44747e9a
//  default: panic(v)
//  }
type AuthAuthorizationClass interface {
	bin.Encoder
	bin.Decoder
	construct() AuthAuthorizationClass
}

Also, the corresponding auth.signIn method:

// AuthSignIn invokes method auth.signIn#bcd51581 returning error if any.
// Signs in a user with a validated phone number.
//
// See https://core.telegram.org/method/auth.signIn for reference.
func (c *Client) AuthSignIn(ctx context.Context, request *AuthSignInRequest) (AuthAuthorizationClass, error) {}

The generated constructors contain detailed official documentation, including links:

// FoldersDeleteFolderRequest represents TL type `folders.deleteFolder#1c295881`.
// Delete a peer folder¹
//
// Links:
//  1) https://core.telegram.org/api/folders#peer-folders
//
// See https://core.telegram.org/method/folders.deleteFolder for reference.
type FoldersDeleteFolderRequest struct {
    // Peer folder ID, for more info click here¹
    //
    // Links:
    //  1) https://core.telegram.org/api/folders#peer-folders
    FolderID int
}

Contributions

Huge thanks to every contributor, dealing with a project of such scale is impossible alone.

Special thanks:

  • tdakkota
    • Two-factor authentication (SRP)
    • Proxy support
    • Update dispatcher
    • Complete transport support (abridged, padded intermediate and full)
    • Telegram server for end-to-end testing
    • Multiple major refactorings, including critical cryptographical scope reduction
    • Code generation improvements (vector support, multiple modes for pretty-print)
    • And many other cool things and performance improvements
  • zweihander
    • Background pings
    • Links in generated documentation
    • Message acknowledgements
    • Retries
    • RPC Engine

Reference

The MTProto protocol description is hosted by Telegram.

Most important parts for client implementations:

Current implementation mostly conforms to security guidelines, but no formal security audit were performed.

Prior art

License

MIT License

Created by Aleksandr (ernado) Razumov

2020

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].