offiaccount.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package util
  2. import (
  3. "fmt"
  4. "github.com/fastwego/offiaccount"
  5. "github.com/fastwego/offiaccount/apis/menu"
  6. "os"
  7. "strings"
  8. )
  9. // 微信公众账号
  10. var OffiAccount *offiaccount.OffiAccount
  11. func init() {
  12. // 加载配置文件
  13. // 创建公众号实例
  14. OffiAccount = offiaccount.New(offiaccount.Config{
  15. Appid: "wxe1cc563ba393dd1a",
  16. Secret: "25e56243da9581eab6f4d67a12ef4658",
  17. //Token: viper.GetString("TOKEN"),
  18. //EncodingAESKey: viper.GetString("EncodingAESKey"),
  19. })
  20. }
  21. func Send_Message2(access_token, msg string) {
  22. app := offiaccount.New(offiaccount.Config{
  23. Appid: "APPID",
  24. Secret: "SECRET",
  25. })
  26. // 调用 api
  27. payload := []byte(`
  28. {
  29. "button":[
  30. {
  31. "name":"菜单",
  32. "sub_button":[
  33. {
  34. "type":"view",
  35. "name":"搜索",
  36. "url":"http://www.baidu.com/"
  37. }]
  38. }]
  39. }`)
  40. resp, err := menu.Create(app, payload)
  41. fmt.Println(resp, err)
  42. }
  43. func messages1(touser string, toparty string, agentid int, content string) string {
  44. msg := MESSAGES{
  45. Touser: touser,
  46. Toparty: toparty,
  47. Msgtype: "text",
  48. Agentid: agentid,
  49. Safe: 0,
  50. Text: struct {
  51. //Subject string `json:"subject"`
  52. Content string `json:"content"`
  53. }{Content: content},
  54. }
  55. sed_msg, _ := json.Marshal(msg)
  56. //fmt.Printf("%s",string(sed_msg))
  57. return string(sed_msg)
  58. }
  59. func main() {
  60. touser := "BigBoss" //企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
  61. toparty := "2" //企业号中的部门id。
  62. agentid := 1000002 //企业号中的应用id。
  63. corpid := "xxxxxxxxxxxxxxxxx" //企业号的标识
  64. corpsecret := "exxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ///企业号中的应用的Secret
  65. accessToken := Get_AccessToken(corpid, corpsecret)
  66. content := os.Args[1]
  67. // fmt.Println(content)
  68. // 序列化成json之后,\n会被转义,也就是变成了\\n,使用str替换,替换掉转义
  69. msg := strings.Replace(messages(touser, toparty, agentid, content), "\\\\", "\\", -1)
  70. // fmt.Println(strings.Replace(msg,"\\\\","\\",-1))
  71. Send_Message(accessToken, msg)
  72. }