telegram bot api
v5.5.1
모든 방법은 상당히 자명하며 Godoc 페이지를 읽으면 모든 것을 설명해야합니다. 분명하지 않은 경우 문제를 열거나 풀 요청을 제출하십시오.
웹 사이트에는 go-telegram-bot-api.dev에 더 많은 튜토리얼과 고급 정보가 있습니다.
이 프로젝트의 범위는 추가 기능없이 API 주변의 래퍼를 제공하는 것입니다. 플러그인과 명령 핸들러로 무언가를 직접 설계 할 필요없이 무언가를 만들기위한 다른 프로젝트가 있습니다.
질문을하거나 개발에 대해 토론하려면 개발 그룹에 가입하십시오.
먼저, 라이브러리가 설치되고 최신 상태 go get -u github.com/go-telegram-bot-api/telegram-bot-api/v5 확인하십시오.
이것은 모든 업데이트를 표시 한 다음 해당 채팅에 답장하는 매우 간단한 봇입니다.
package main
import (
"log"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func main () {
bot , err := tgbotapi . NewBotAPI ( "MyAwesomeBotToken" )
if err != nil {
log . Panic ( err )
}
bot . Debug = true
log . Printf ( "Authorized on account %s" , bot . Self . UserName )
u := tgbotapi . NewUpdate ( 0 )
u . Timeout = 60
updates := bot . GetUpdatesChan ( u )
for update := range updates {
if update . Message != nil { // If we got a message
log . Printf ( "[%s] %s" , update . Message . From . UserName , update . Message . Text )
msg := tgbotapi . NewMessage ( update . Message . Chat . ID , update . Message . Text )
msg . ReplyToMessageID = update . Message . MessageID
bot . Send ( msg )
}
}
}WebHooks (Google App Engine에서 실행하려는 경우)를 사용해야하는 경우 약간 다른 방법을 사용할 수 있습니다.
package main
import (
"log"
"net/http"
"github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func main () {
bot , err := tgbotapi . NewBotAPI ( "MyAwesomeBotToken" )
if err != nil {
log . Fatal ( err )
}
bot . Debug = true
log . Printf ( "Authorized on account %s" , bot . Self . UserName )
wh , _ := tgbotapi . NewWebhookWithCert ( "https://www.example.com:8443/" + bot . Token , "cert.pem" )
_ , err = bot . Request ( wh )
if err != nil {
log . Fatal ( err )
}
info , err := bot . GetWebhookInfo ()
if err != nil {
log . Fatal ( err )
}
if info . LastErrorDate != 0 {
log . Printf ( "Telegram callback failed: %s" , info . LastErrorMessage )
}
updates := bot . ListenForWebhook ( "/" + bot . Token )
go http . ListenAndServeTLS ( "0.0.0.0:8443" , "cert.pem" , "key.pem" , nil )
for update := range updates {
log . Printf ( "%+v n " , update )
}
}필요한 경우 HTTPS / TLS가 필요하므로 자체 서명 된 인증서를 생성 할 수 있습니다. 위의 예는 Telegram에 이것이 귀하의 인증서이며 제대로 서명하지 않더라도 신뢰해야한다고 알려줍니다.
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3560 -subj "//O=OrgCN=Test" -nodes
이제 암호화를 이용할 수 있으므로 무료 TLS 인증서를 생성 할 수 있습니다.