GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bot/common.go at main · go-telegram/bot

Expand file tree

/

common.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
140 lines (124 loc) · 3.73 KB
/

common.go

Copy path

File metadata and controls

140 lines (124 loc) · 3.73 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package bot
import (
"crypto/hmac"
"encoding/json"
"math/rand"
"net/url"
"strings"
"sync"
"time"
)
const shouldBeEscaped = "_*[]()~`>#+-=|{}.!"
// EscapeMarkdown escapes plain text for Telegram MarkdownV2 syntax.
// Use it for user input and other dynamic text.
// For example, EscapeMarkdown("Hello_world") returns "Hello\_world".
func EscapeMarkdown(s string) string {
var result []rune
for _, r := range s {
if r == '\\' || strings.ContainsRune(shouldBeEscaped, r) {
result = append(result, '\\')
}
result = append(result, r)
}
return string(result)
}
// EscapeMarkdownUnescaped escapes special symbols that are not already escaped
// for Telegram MarkdownV2 syntax. Use it for text that may already contain
// MarkdownV2 escapes.
func EscapeMarkdownUnescaped(s string) string {
var result []rune
var escaped bool
for _, r := range s {
if r == '\\' {
escaped = !escaped
result = append(result, r)
continue
}
if strings.ContainsRune(shouldBeEscaped, r) && !escaped {
result = append(result, '\\')
}
escaped = false
result = append(result, r)
}
return string(result)
}
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
var randSrc = rand.NewSource(time.Now().UnixNano())
var randSrcMx sync.Mutex
// RandomString returns random a-zA-Z string with n length
func RandomString(n int) string {
b := make([]byte, n)
// A randSrc.Int63() generates 63 random bits, enough for letterIdxMax characters!
randSrcMx.Lock()
ch := randSrc.Int63()
randSrcMx.Unlock()
for i, remain := n-1, letterIdxMax; i >= 0; {
if remain == 0 {
randSrcMx.Lock()
ch, remain = randSrc.Int63(), letterIdxMax
randSrcMx.Unlock()
}
if idx := int(ch & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
ch >>= letterIdxBits
remain--
}
return string(b)
}
// WebAppUser represents user model from webapp request
type WebAppUser struct {
ID int64 `json:"id"`
IsBot bool `json:"is_bot"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Username string `json:"username"`
LanguageCode string `json:"language_code"`
IsPremium bool `json:"is_premium"`
AddedToAttachmentMenu bool `json:"added_to_attachment_menu"`
AllowsWriteToPM bool `json:"allows_write_to_pm"`
PhotoURL string `json:"photo_url"`
}
// ValidateWebappRequest validates Mini App init data.
// values must already be decoded, as returned by url.ParseQuery or http.Request.URL.Query.
// It uses the first value per key and leaves values unchanged.
// An empty token, LF in signed names or values, or '=' in signed names returns nil, false.
func ValidateWebappRequest(values url.Values, token string) (user *WebAppUser, ok bool) {
fields := prepareWebAppRequest(values, token)
if fields == nil {
return nil, false
}
expected := webAppHMAC(token, webAppDataCheckString(fields))
if !hmac.Equal([]byte(expected), []byte(values.Get("hash"))) {
return nil, false
}
var u WebAppUser
if v, ok := fields["user"]; ok {
if err := json.Unmarshal([]byte(v), &u); err != nil {
return nil, false
}
}
return &u, true
}
func prepareWebAppRequest(values url.Values, token string) map[string]string {
if token == "" {
return nil
}
fields := make(map[string]string, len(values))
for k, v := range values {
if k == "hash" {
continue
}
if len(v) == 0 || !isUnambiguousWebAppField(k, v[0]) {
return nil
}
fields[k] = v[0]
}
return fields
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.