Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create helper function WriteToHTTPResponse #312

Merged
merged 1 commit into from Mar 19, 2020
Merged

Create helper function WriteToHTTPResponse #312

merged 1 commit into from Mar 19, 2020

Conversation

CNA-Bld
Copy link
Contributor

@CNA-Bld CNA-Bld commented Mar 2, 2020

This helper function makes it easier to use webhook feature "Making requests when getting updates".

This makes it faster to write simple and stupid bots, especially with serverless deployment. For instance, an inline Markdown bot only needs 15 LOC, without even using a BotAPI instance, and without sending any outgoing HTTP requests:

package main

import (
	"encoding/json"
	"io/ioutil"
	"net/http"

	"github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

func DemoWebhook(w http.ResponseWriter, r *http.Request) {
	bytes, _ := ioutil.ReadAll(r.Body)

	var update tgbotapi.Update
	json.Unmarshal(bytes, &update)

	if update.InlineQuery != nil && update.InlineQuery.Query != "" {
		tgbotapi.WriteToHTTPResponse(w, tgbotapi.InlineConfig{
			InlineQueryID: update.InlineQuery.ID,
			Results: []interface{}{
				tgbotapi.NewInlineQueryResultArticleMarkdown("id", "Markdown", update.InlineQuery.Query),
			},
		})
	}
}

func main() {
	http.HandleFunc("/", DemoWebhook)
	http.ListenAndServe(":8080", nil)
}

@zhuharev zhuharev merged commit 6ce4ec7 into go-telegram-bot-api:develop Mar 19, 2020
@CNA-Bld CNA-Bld deleted the write-to-http-response branch March 19, 2020 08:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants