Giter VIP home page Giter VIP logo

mattermost-bot-sample-golang's Introduction

Mattermost Bot Sample

Overview

This sample Bot shows how to use the Mattermost Go driver to interact with a Mattermost server, listen to events and respond to messages. Documentation for the Go driver can be found here.

Highlights of APIs used in this sample:

  • Log in to the Mattermost server
  • Create a channel
  • Modify user attributes
  • Connect and listen to WebSocket events for real-time responses to messages
  • Post a message to a channel

This Bot Sample was tested with Mattermost server version 7.5.2.

Setup Server Environment

Via Docker And Docker-Compose

1 - Ensure Docker and Docker-Compose are installed on your system.

2 - Run docker-compose up -d --build and the mattermost client will be built and will expose the port 8065 to your system's localhost.

3 - Run ./add_users.sh. The login information for the Mattermost client will be printed.

4 - Log into your mattermost instance using these credentials and create a bot account following these instructions.

5 - Copy example.env to .env and fill in the bot token (obtained from the previous step), team name, etc. Alternatively, just provide your credentials as environment variables.

5 - Start the Bot.

make run

You can verify the Bot is running when

  • Logged in to mattermost appears on the command line.
  • Hi! I am a bot. is posted in your specified channel.

See "Test the Bot" for testing instructions

Via Direct System Install/Setup

1 - Install or upgrade to Mattermost server version 3.10+, and verify that the Mattermost server is running on http://localhost:8065.

On the commands below, if you are running Mattermost server version 5.0 or later, use ./bin/mmctl. If you are running version 4.10 or earlier, use ./bin/platform.

Learn more about the mmctl CLI tool in the Mattermost documentation.

2 - Create a team for the Bot to run. If you have an existing team, you may skip this step and replace botsample with your existing team in subsequent steps.

./bin/mmctl team create --name botsample --display_name "Sample Bot playground" --email "[email protected]"

3 - Create a user, bill, which we will use to log in and interact with the Bot.

./bin/mmctl user create --email="[email protected]" --password="Password1!" --username="bill"

4 - (Optional) Give bill system_admin permissions.

./bin/mmctl roles system_admin bill

5 - Log in to http://localhost:8065 as bill and verify the account was created successfully.

6 - Create a bot account following these instructions.

7 - Copy example.env to .env and fill in the bot token (obtained from the previous step), team name, etc. Alternatively, just provide your credentials as environment variables.

Setup Bot Development Environment

1 - Follow the Developer Machine Setup instructions to setup the bot development environment.

2 - Clone the GitHub repository to run the sample.

git clone https://github.com/mattermost/mattermost-bot-sample-golang.git
cd mattermost-bot-sample-golang

3 - Log into your mattermost instance using these credentials and create a bot account following these instructions.

4 - Copy example.env to .env and fill in the bot token (obtained from the previous step), team name, etc. Alternatively, just provide your credentials as environment variables.

5 - Run with

make run

You can verify the Bot is running when

  • Logged in to mattermost appears on the command line.
  • Hi! I am a bot. is posted in your specified channel.

Test the Bot

1 - Log in to the Mattermost server as [email protected] and Password1!.

2 - Join your specified channel.

3 - Post a message in the channel such as hello? to see if the Bot responds. You should see a response if the Bot is running.

Stop the Bot

1 - In the terminal window, press CTRL+C to stop the bot.

mattermost-bot-sample-golang's People

Contributors

anon6789 avatar asutosh97 avatar catalintomai avatar coreyhulen avatar crspeller avatar fungos avatar hmhealey avatar icewreck avatar it33 avatar jasonblais avatar lushan01 avatar mickmister avatar musikpolice avatar pneisen avatar saf6260 avatar streamer45 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mattermost-bot-sample-golang's Issues

Unable to run mattermost bot

Hello,

I seem to be having an issue running my mattermost bot.
When I type the command "make run" it gives me the error "/bin/sh: 1: go: not found make: *** [run] Error 127".
However, when I do a ls I see the go file in the directory.
Does anyone have any advice?

Thanks.

  • Victoria
    make run

Unresponsive after some time

This sample bot stops responding if left running for a while.
I found an issue here which details the same behavior and it was fixed by restarting the websocket connection when an error is detected.

This logic should be added to the sample to prevent newbies from hitting the same issue.

Can't build the example bot

After following the relevant instructions,

git clone https://github.com/mattermost/mattermost-bot-sample-golang.git

cd mattermost-bot-sample-golang

then I did this

glide up

the build fails with

dschulz@testserver:~/mattermost-bot-sample-golang$ make run
go run *.go
# command-line-arguments
./bot_sample.go:203: event.ChannelId undefined (type *model.WebSocketEvent has no field or method ChannelId)
./bot_sample.go:213: event.UserId undefined (type *model.WebSocketEvent has no field or method UserId)
Makefile:15: recipe for target 'run' failed
make: *** [run] Error 2

What am I doing wrong?

License missing

What license is this sample? The headers only say:

// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

Problem with server update to 5.1

Updated a server to 5.1 and my bot is failing on the socket call:

webSocketClient, err := model.NewWebSocketClient("wss://"+MattermostHost, client.AuthToken) if err != nil { println("We failed to connect to the web socket") fmt.Println(err) }

In the console I see:

We failed to connect to the web socket NewWebSocketClient: model.websocket_client.connect_fail.app_error, websocket: bad handshake

Writing out the resp in NewWebSocketClient shows:

`
wss:///api/v4/users/websocket

&{401 Unauthorized 401 HTTP/1.1 1 1 map[X-Ratelimit-Limit:[1001] X-Frame-Options:[SAMEORIGIN] Content-Type:[application/json] Expires:[0] X-Ratelimit-Reset:[1] Date:[Wed, 25 Jul 2018 19:06:44 GMT] X-Request-Id:[nnbx46s4rtyk8jp4oqbt9pz8cr] Content-Length:[186] X-Ratelimit-Remaining:[999] X-Version-Id:[4.2.0.4.2.0.41509822b940251c088c73d7081937bb.false] Server:[nginx/1.12.1] Content-Security-Policy:[frame-ancestors 'self';]] {0xc42016e888} 186 [] true false map[] 0xc420108100 }
`

Tried on a server still running 4.2 and it works as expected. Did I miss something in the upgrade? Thanks.

No attachments have been sent

hello ,I look at you sample,but it don't give me a sample about sending attachments .can you give a sample with it,thank you!

Unable to make bot in mattermost

./bin/platform team create --name botsample --display_name "Sample Bot playground" --email "[email protected]"

output : botsample --display_name "Sample Bot playground" --email "[email protected]" --debug
-bash: ./bin/platform: No such file or directory

running the latest version of mattermost server on localhost:8605. Followed the Readmd file with no luck. One youtube video shows mattermost/platform repo which I was unable to find. Can someone guide.

File upload case

Apart from text,Please include case on listing file information when file is uploaded.

WSS retry

Hi,

I'm using a part of this bot sample in an internal bot we use and I face an issue,

after few delay, the wss notification stop to be received to the bot.

I think it's located on this part of the code base :

 49         webSocketClient.Listen()                                                                                   
 50         go func() {                                                                                                
 51                 for {                                                                                              
 52                         select {                                                                                   
 53                         case resp := <-webSocketClient.EventChannel:                                               
 54                                 HandleWebSocketResponse(resp)                                                      
 55                         }                                                                                          
 56                 }                                                                                                  
 57         }()     

I think in the for loop I need to add a check to ensure that the WSS is still connected and reconnect it if needed.

Can you have a recommandation on how to achieve that ?

Thanks a lot Mattermost team & folks !

Direct Messages?

Is it possible to call the client and retrieve a list of direct message options? I can get channels via:

x, y := client.SearchChannels(oteam.Id, search)
fmt.Println(y)
for _, t := range x {
   println(t.Name + "  :  " + t.Id)
}

But this does not return direct message channels. I would like the bot to be able to respond to 1:1 commands as well.

Thanks.

Login with gitlab SSO

Is it possible to login using gitlab single sign on? I'm using the GitLab omnibus edition that ships with mattermost. Users currently log in to mattermost via a "Sign in with gitlab" page which in turn uses LDAP.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.