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

Use proxy for http client? #240

Closed
ahfa92 opened this issue Oct 31, 2023 · 3 comments
Closed

Use proxy for http client? #240

ahfa92 opened this issue Oct 31, 2023 · 3 comments
Assignees
Labels
question Further information is requested

Comments

@ahfa92
Copy link

ahfa92 commented Oct 31, 2023

How can we use proxy server for this package?

In an environment which is very strict, we can not simply use http_proxy for global application. Is there any way to route the url via proxy server e.g. squid proxy?

Thank you in advance!!

@atc0005 atc0005 added question Further information is requested help wanted Extra attention is needed labels Oct 31, 2023
@atc0005
Copy link
Owner

atc0005 commented Oct 31, 2023

Hi @ahfa92,

I've not tried that before, but I found what I think is a good explanation here:

Here is an untested example based on the existing adaptivecard/basic example:

package main

import (
	"log"
	"net/http"
	"net/url"
	"os"

	goteamsnotify "github.com/atc0005/go-teams-notify/v2"
	"github.com/atc0005/go-teams-notify/v2/adaptivecard"
)

func main() {

	// Initialize a new Microsoft Teams client.
	mstClient := goteamsnotify.NewTeamsClient()

	proxyURLString := "http://proxyIp:proxyPort"
	proxyUrl, err := url.Parse(proxyURLString)

	if err != nil {
		log.Printf(
			"failed to parse proxy URL %q: %v",
			proxyURLString,
			err,
		)
		os.Exit(1)
	}

	httpClient := mstClient.HTTPClient()
	httpClient.Transport = &http.Transport{Proxy: http.ProxyURL(proxyUrl)}

	// Set webhook url.
	webhookUrl := "https://outlook.office.com/webhook/YOUR_WEBHOOK_URL_OF_TEAMS_CHANNEL"

	// The title for message (first TextBlock element).
	msgTitle := "Hello world"

	// Formatted message body.
	msgText := "Here are some examples of formatted stuff like " +
		"\n * this list itself  \n * **bold** \n * *italic* \n * ***bolditalic***"

	// Create message using provided formatted title and text.
	msg, err := adaptivecard.NewSimpleMessage(msgText, msgTitle, true)
	if err != nil {
		log.Printf(
			"failed to create message: %v",
			err,
		)
		os.Exit(1)
	}

	// Send the message with default timeout/retry settings.
	if err := mstClient.Send(webhookUrl, msg); err != nil {
		log.Printf(
			"failed to send message: %v",
			err,
		)
		os.Exit(1)
	}
}

Feedback is welcome.

@atc0005 atc0005 removed the help wanted Extra attention is needed label Oct 31, 2023
@atc0005 atc0005 self-assigned this Oct 31, 2023
@ahfa92
Copy link
Author

ahfa92 commented Nov 1, 2023

I will try to use this method. Will inform you later. Thanks!

@ahfa92
Copy link
Author

ahfa92 commented Nov 1, 2023

The solution is working. Thank you!!

@ahfa92 ahfa92 closed this as completed Nov 1, 2023
atc0005 added a commit that referenced this issue Nov 3, 2023
Add examples of specifying a proxy server for:

- MessageCard format
- Adpative Card format

Thanks to @ahfa92 for the idea and testing feedback.

refs GH-240
atc0005 added a commit that referenced this issue Nov 3, 2023
Add examples of specifying a proxy server for:

- MessageCard format
- Adpative Card format

Thanks to @ahfa92 for the idea and testing feedback.

refs GH-240
atc0005 added a commit that referenced this issue Nov 3, 2023
Add examples of specifying a proxy server for:

- MessageCard format
- Adpative Card format

Thanks to @ahfa92 for the idea and testing feedback.

refs GH-240
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants