html

package
v0.0.0-...-87e9d67 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package html contains HTML styling options.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes

func Bytes(resolver func(id int64) (tg.InputUserClass, error), b []byte) styling.StyledTextOption

Bytes reads HTML from given byte slice and returns styling option to build styled text block.

func Format

func Format(resolver func(id int64) (tg.InputUserClass, error), format string, args ...interface{}) styling.StyledTextOption

Format formats string using fmt, parses HTML from formatted string and returns styling option to build styled text block.

func HTML

func HTML(r io.Reader, b *entity.Builder, opts Options) error

HTML parses given input from reader and adds parsed entities to given builder. Notice that this parser ignores unsupported tags.

Parameter userResolver is used to resolve user by ID during formatting. May be nil. If userResolver is nil, formatter will create tg.InputUser using only ID. Notice that it's okay for bots, but not for users.

See https://core.telegram.org/bots/api#html-style.

func Reader

func Reader(resolver func(id int64) (tg.InputUserClass, error), r io.Reader) styling.StyledTextOption

Reader reads HTML from given reader and returns styling option to build styled text block.

func String

func String(resolver func(id int64) (tg.InputUserClass, error), s string) styling.StyledTextOption

String reads HTML from given string and returns styling option to build styled text block.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	"bitbucket.org/hokego/hokego-td/telegram"
	"bitbucket.org/hokego/hokego-td/telegram/message"
	"bitbucket.org/hokego/hokego-td/telegram/message/html"
	"bitbucket.org/hokego/hokego-td/tg"
)

func sendHTML(ctx context.Context) error {
	client, err := telegram.ClientFromEnvironment(telegram.Options{})
	if err != nil {
		return err
	}

	// This example creates a styled message from BotAPI examples
	// and sends to your Saved Messages folder.
	// See https://core.telegram.org/bots/api#html-style.
	return client.Run(ctx, func(ctx context.Context) error {
		_, err := message.NewSender(tg.NewClient(client)).
			Self().StyledText(ctx, html.String(nil, `<b>bold</b>, <strong>bold</strong>
<i>italic</i>, <em>italic</em>
<u>underline</u>, <ins>underline</ins>
<s>strikethrough</s>, <strike>strikethrough</strike>, <del>strikethrough</del>
<b>bold <i>italic bold <s>italic bold strikethrough</s> <u>underline italic bold</u></i> bold</b>
<a href="http://www.example.com/">inline URL</a>
<a href="tg://user?id=123456789">inline mention of a user</a>
<code>inline fixed-width code</code>
<pre>pre-formatted fixed-width code block</pre>
<pre><code class="language-python">pre-formatted fixed-width code block written in the Python programming language</code></pre>`))
		return err
	})
}

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	if err := sendHTML(ctx); err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(2)
	}
}
Output:

Types

type Options

type Options struct {
	// UserResolver is used to resolve user by ID during formatting. May be nil.
	//
	// If userResolver is nil, formatter will create tg.InputUser using only ID.
	// Notice that it's okay for bots, but not for users.
	UserResolver entity.UserResolver
	// DisableTelegramEscape disable Telegram BotAPI escaping and uses default
	// golang.org/x/net/html escape.
	DisableTelegramEscape bool
}

Options is options of HTML.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL