Skip to content

Commit 07092db

Browse files
Stanislav Ochotnickýfabioxgn
authored andcommitted
Add hangouts chat support (go-chat-bot#96)
1 parent a6d0779 commit 07092db

File tree

5 files changed

+341
-0
lines changed

5 files changed

+341
-0
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,62 @@ func main() {
169169
}
170170
```
171171

172+
### Google Chat
173+
174+
To deploy your go-bot to Google Chat (also known as Hangouts Chat, not plain
175+
Hangouts) you will first need to follow documentation to [setup pub/sub
176+
project](https://developers.google.com/hangouts/chat/how-tos/pub-sub) in Google
177+
Cloud. This will enable your bot to receive messages even when it is behind a
178+
firewall.
179+
180+
Condensed, the steps you will need to take are as follows:
181+
* Create new project in google cloud console
182+
* ID of the project will be used in Config.PubSubProject
183+
* Create service credentials for this project
184+
* Path to downloaded credentials file should be in env variable GOOGLE_APPLICATION_CREDENTIALS
185+
* Choose "Pub/Sub Editor" role for the credential
186+
* Enable Pub/Sub API in cloud console
187+
* Create new topic in the Pub/Sub (say "google-chat")
188+
* This is Config.TopicName
189+
* Modify permissions on created topic so that
190+
"[email protected]" has Pub/Sub Publisher permissions
191+
* Enable hangouts chat api in Cloud Console
192+
* Go to hangouts chat API config in the Cloud Console and fill in info
193+
* Connection settings - use Pub/Sub and fill in topic string you created
194+
above
195+
* Verification token is your Config.Token
196+
197+
Config.SubscriptionName should be unique for each environment or you'll not
198+
process messages correctly. If you encounter issues make sure your credentials
199+
are correct and permissions for topics/queues are set up correctly.
200+
201+
Config.WelcomeMessage is sent each time the bot joins a new room or private chat.
202+
203+
Full example is here:
204+
```Go
205+
package main
206+
207+
import (
208+
"os"
209+
210+
"github.com/go-chat-bot/bot/google-chat"
211+
_ "github.com/go-chat-bot/plugins/godoc"
212+
_ "github.com/go-chat-bot/plugins/catfacts"
213+
_ "github.com/go-chat-bot/plugins/catgif"
214+
_ "github.com/go-chat-bot/plugins/chucknorris"
215+
)
216+
217+
func main() {
218+
googlechat.Run(&googlechat.Config{
219+
PubSubProject: os.Getenv("HANGOUTS_PROJECT"),
220+
TopicName: os.Getenv("HANGOUTS_TOPIC"),
221+
SubscriptionName: os.Getenv("HANGOUTS_SUB"),
222+
WelcomeMessage: os.Getenv("HANGOUTS_WELCOME"),
223+
Token: os.Getenv("HANGOUTS_TOKEN")})
224+
}
225+
226+
```
227+
172228
## Deploying your own bot
173229

174230
To see an example project on how to deploy your bot, please see my own configuration:

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
module github.com/go-chat-bot/bot
22

33
require (
4+
cloud.google.com/go v0.32.0
45
github.com/andygrunwald/go-jira v1.5.0 // indirect
56
github.com/beevik/guid v0.0.0-20170504223318-d0ea8faecee0 // indirect
67
github.com/cloudfoundry/gosigar v1.1.0 // indirect
78
github.com/fatih/structs v1.0.0 // indirect
89
github.com/go-chat-bot/plugins v0.0.0-20181006134258-491b3f9878d6
910
github.com/go-chat-bot/plugins-br v0.0.0-20170316122923-eb41b30907dc
1011
github.com/google/go-querystring v1.0.0 // indirect
12+
github.com/googleapis/gax-go v2.0.0+incompatible // indirect
1113
github.com/gorilla/websocket v1.4.0 // indirect
1214
github.com/martinusso/go-docs v0.0.0-20161215163720-81905d575a58 // indirect
1315
github.com/mattn/go-shellwords v1.0.3
@@ -19,6 +21,12 @@ require (
1921
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
2022
github.com/thoj/go-ircevent v0.0.0-20180816043103-14f3614f28c3
2123
github.com/trivago/tgo v1.0.5 // indirect
24+
go.opencensus.io v0.18.0 // indirect
25+
golang.org/x/net v0.0.0-20181108082009-03003ca0c849 // indirect
26+
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
27+
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f // indirect
2228
golang.org/x/sys v0.0.0-20181005133103-4497e2df6f9e // indirect
29+
google.golang.org/api v0.0.0-20181108001712-cfbc873f6b93 // indirect
30+
google.golang.org/genproto v0.0.0-20181109154231-b5d43981345b // indirect
2331
gopkg.in/telegram-bot-api.v3 v3.0.0
2432
)

go.sum

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,52 @@
1+
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
2+
cloud.google.com/go v0.32.0 h1:DSt59WoyNcfAInilEpfvm2ugq8zvNyaHAm9MkzOwRQ4=
3+
cloud.google.com/go v0.32.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
4+
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
15
github.com/andygrunwald/go-jira v1.5.0 h1:/1CyYLNdwus7TvB/DHyD3udb52K12aYL9m7WaGAO9m4=
26
github.com/andygrunwald/go-jira v1.5.0/go.mod h1:yNYQrX3nGSrVdcVsM2mWz2pm7tTeDtYfRyVEkc3VUiY=
37
github.com/beevik/guid v0.0.0-20170504223318-d0ea8faecee0 h1:oLd/YLOTOgA4D4aAUhIE8vhl/LAP1ZJrj0mDQpl7GB8=
48
github.com/beevik/guid v0.0.0-20170504223318-d0ea8faecee0/go.mod h1:XzXWuOd1wJ63MtICHh5+PnvCuxsB/d58T8TswEhI/9I=
9+
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
10+
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
511
github.com/cloudfoundry/gosigar v1.1.0 h1:V/dVCzhKOdIU3WRB5inQU20s4yIgL9Dxx/Mhi0SF8eM=
612
github.com/cloudfoundry/gosigar v1.1.0/go.mod h1:3qLfc2GlfmwOx2+ZDaRGH3Y9fwQ0sQeaAleo2GV5pH0=
713
github.com/fatih/structs v1.0.0 h1:BrX964Rv5uQ3wwS+KRUAJCBBw5PQmgJfJ6v4yly5QwU=
814
github.com/fatih/structs v1.0.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
15+
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
916
github.com/go-chat-bot/plugins v0.0.0-20181006134258-491b3f9878d6 h1:qNYjVQnDwznjLk+OnNdczA5SXwEa/RwjPTZSQCKofF4=
1017
github.com/go-chat-bot/plugins v0.0.0-20181006134258-491b3f9878d6/go.mod h1:Ga63x4EC4NFYr/KGzhn8D8fLj89sfJA/dpBsuowiHOQ=
1118
github.com/go-chat-bot/plugins-br v0.0.0-20170316122923-eb41b30907dc h1:v/poG4Y4O/z1cUm2cWxiIkFFgRsT3Fe1u1A33evx89g=
1219
github.com/go-chat-bot/plugins-br v0.0.0-20170316122923-eb41b30907dc/go.mod h1:KU0Ieo/D/HBwPY6n3tLWanM5GemW6iWXdbgm96qRW2Q=
20+
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
21+
github.com/golang/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E=
22+
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
23+
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
24+
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
25+
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
1326
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
1427
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
28+
github.com/googleapis/gax-go v2.0.0+incompatible h1:j0GKcs05QVmm7yesiZq2+9cxHkNK9YM6zKx4D2qucQU=
29+
github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY=
1530
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
1631
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
32+
github.com/grpc-ecosystem/grpc-gateway v1.5.0/go.mod h1:RSKVYQBd5MCa4OVpNdGskqpgL2+G+NZTnrVHpWWfpdw=
33+
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
1734
github.com/martinusso/go-docs v0.0.0-20161215163720-81905d575a58 h1:VmcrkkMjTdCGOsuuMnn7P2X9dGh3meUNASx6kHIpe7A=
1835
github.com/martinusso/go-docs v0.0.0-20161215163720-81905d575a58/go.mod h1:QymHbiLXXhrSGV5xTWYfEBt9mau3hHwVOT9Y7tpolJU=
1936
github.com/mattn/go-shellwords v1.0.3 h1:K/VxK7SZ+cvuPgFSLKi5QPI9Vr/ipOf4C1gN+ntueUk=
2037
github.com/mattn/go-shellwords v1.0.3/go.mod h1:3xCvwCdWdlDJUrvuMn7Wuy9eWs4pE8vqg+NOMyg4B2o=
38+
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
2139
github.com/mozillazg/go-unidecode v0.1.0 h1:wAIMDf/yTexXKxT5TkctLwmClGSyuoJaZDRMclFNq8U=
2240
github.com/mozillazg/go-unidecode v0.1.0/go.mod h1:fYMdhyjni9ZeEmS6OE/GJHDLsF8TQvIVDwYR/drR26Q=
2341
github.com/nlopes/slack v0.4.0 h1:OVnHm7lv5gGT5gkcHsZAyw++oHVFihbjWbL3UceUpiA=
2442
github.com/nlopes/slack v0.4.0/go.mod h1:jVI4BBK3lSktibKahxBF74txcK2vyvkza1z/+rRnVAM=
43+
github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=
2544
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
2645
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
46+
github.com/prometheus/client_golang v0.8.0/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
47+
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
48+
github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
49+
github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
2750
github.com/pyinx/gorocket v0.0.0-20170810024322-78ae1353729f h1:N1r6pSlez3lLsqaNHbtrHW9ZuzrilETIabr9jPNj3Zs=
2851
github.com/pyinx/gorocket v0.0.0-20170810024322-78ae1353729f/go.mod h1:nh/AiOs8vRCaqnSOHVzyta23ZLm5ck/st4brrxtQJEo=
2952
github.com/robfig/cron v0.0.0-20180505203441-b41be1df6967 h1:x7xEyJDP7Hv3LVgvWhzioQqbC/KtuUhTigKlH/8ehhE=
@@ -34,7 +57,39 @@ github.com/thoj/go-ircevent v0.0.0-20180816043103-14f3614f28c3 h1:389FrrKIAlxqQM
3457
github.com/thoj/go-ircevent v0.0.0-20180816043103-14f3614f28c3/go.mod h1:QYOctLs5qEsaIrA/PKEc4YqAv2SozbxNEX0vMPs84p4=
3558
github.com/trivago/tgo v1.0.5 h1:ihzy8zFF/LPsd8oxsjYOE8CmyOTNViyFCy0EaFreUIk=
3659
github.com/trivago/tgo v1.0.5/go.mod h1:w4dpD+3tzNIIiIfkWWa85w5/B77tlvdZckQ+6PkFnhc=
60+
go.opencensus.io v0.18.0 h1:Mk5rgZcggtbvtAun5aJzAtjKKN/t0R3jJPlWILlv938=
61+
go.opencensus.io v0.18.0/go.mod h1:vKdFvxhtzZ9onBp9VKHK8z/sRpBMnKAsufL7wlDrCOA=
62+
golang.org/x/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
63+
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
64+
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
65+
golang.org/x/net v0.0.0-20181106065722-10aee1819953/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
66+
golang.org/x/net v0.0.0-20181108082009-03003ca0c849 h1:FSqE2GGG7wzsYUsWiQ8MZrvEd1EOyU3NCF0AW3Wtltg=
67+
golang.org/x/net v0.0.0-20181108082009-03003ca0c849/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
68+
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs=
69+
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
70+
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
71+
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTmV7VDcZyvRZ+QQXkXTZQ=
72+
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
73+
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
74+
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
3775
golang.org/x/sys v0.0.0-20181005133103-4497e2df6f9e h1:EfdBzeKbFSvOjoIqSZcfS8wp0FBLokGBEs9lz1OtSg0=
3876
golang.org/x/sys v0.0.0-20181005133103-4497e2df6f9e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
77+
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
78+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
79+
golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
80+
google.golang.org/api v0.0.0-20180910000450-7ca32eb868bf/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
81+
google.golang.org/api v0.0.0-20181108001712-cfbc873f6b93 h1:TSNRyeWkPS9y+f2F+lO1Yfn9VaFA8MGMUPvn4dXqia0=
82+
google.golang.org/api v0.0.0-20181108001712-cfbc873f6b93/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
83+
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
84+
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
85+
google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
86+
google.golang.org/genproto v0.0.0-20181109154231-b5d43981345b h1:WkFtVmaZoTRVoRYr0LTC9SYNhlw0X0HrVPz2OVssVm4=
87+
google.golang.org/genproto v0.0.0-20181109154231-b5d43981345b/go.mod h1:7Ep/1NZk928CDR8SjdVbjWNpdIf6nzjE3BTgJDr2Atg=
88+
google.golang.org/grpc v1.14.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
89+
google.golang.org/grpc v1.16.0 h1:dz5IJGuC2BB7qXR5AyHNwAUBhZscK2xVez7mznh72sY=
90+
google.golang.org/grpc v1.16.0/go.mod h1:0JHn/cJsOMiMfNA9+DeHDlAU7KAAB5GDlYFpa9MZMio=
91+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3992
gopkg.in/telegram-bot-api.v3 v3.0.0 h1:Y6QmqOMwRKv5NUdlvzEBtEZChjsrqdTS6O858cvuCww=
4093
gopkg.in/telegram-bot-api.v3 v3.0.0/go.mod h1:WxP4rAHcQNrXhQLGIK9aVLkpygV4Qq8YS3yjjJ/0VLA=
94+
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
95+
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

google-chat/chat_msg.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package googlechat
2+
3+
import "time"
4+
5+
// ChatMessage is message type from Pub/Sub events
6+
type ChatMessage struct {
7+
Type string `json:"type"`
8+
EventTime time.Time `json:"eventTime"`
9+
Token string `json:"token"`
10+
Message struct {
11+
Name string `json:"name"`
12+
Sender struct {
13+
Name string `json:"name"`
14+
DisplayName string `json:"displayName"`
15+
AvatarURL string `json:"avatarUrl"`
16+
Email string `json:"email"`
17+
Type string `json:"type"`
18+
} `json:"sender"`
19+
CreateTime time.Time `json:"createTime"`
20+
Text string `json:"text"`
21+
Thread struct {
22+
Name string `json:"name"`
23+
RetentionSettings struct {
24+
State string `json:"state"`
25+
} `json:"retentionSettings"`
26+
} `json:"thread"`
27+
Space struct {
28+
Name string `json:"name"`
29+
Type string `json:"type"`
30+
} `json:"space"`
31+
ArgumentText string `json:"argumentText"`
32+
} `json:"message"`
33+
User struct {
34+
Name string `json:"name"`
35+
DisplayName string `json:"displayName"`
36+
AvatarURL string `json:"avatarUrl"`
37+
Email string `json:"email"`
38+
Type string `json:"type"`
39+
} `json:"user"`
40+
Space struct {
41+
Name string `json:"name"`
42+
Type string `json:"type"`
43+
DisplayName string `json:"displayName"`
44+
} `json:"space"`
45+
ConfigCompleteRedirectURL string `json:"configCompleteRedirectUrl"`
46+
}
47+
48+
// ReplyThread is a part of reply messages
49+
type ReplyThread struct {
50+
Name string `json:"name,omitempty"`
51+
}
52+
53+
// ReplyMessage is partial hangouts format of messages used
54+
// For details see
55+
// https://developers.google.com/hangouts/chat/reference/rest/v1/spaces.messages#Message
56+
type ReplyMessage struct {
57+
Text string `json:"text"`
58+
Thread *ReplyThread `json:"thread,omitempty"`
59+
}

0 commit comments

Comments
 (0)