telegram bot api
v5.5.1
すべての方法はかなり自明であり、Godocページを読むことはすべてを説明する必要があります。何かが明確でない場合は、問題を開くか、プルリクエストを送信してください。
ウェブサイトには、より多くのチュートリアルと高レベルの情報があります。
このプロジェクトの範囲は、追加機能なしで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
Let's Encryptが利用可能になったので、そこで無料のTLS証明書を生成することをお勧めします。