This is a creation in Article, where the information may have evolved or changed.
Getting started with a fun project is a great place to start a new language.
I am also a small robot that can chat, can set a memo/timer reminder, can search the features of the American drama/movie Resources and so on, quickly mastered the language of go and like it.
At the end of the article will give the small robot source code and the text of the example codes link.
1. Set a small goal-start with the dialogue
Let the robot "speak" is the first, where the third-party services provided by the API, the local through the HTTP request to get answers and return.
This stage is basically no difficulty, take go for example, the key part of the dozens of lines of code to solve:
//get reply from tlAIfunc tlAI(info string) string { tuLingURL := fmt.Sprintf("http://www.tuling123.com/openapi/api?key=%s&info=%s", tlKey, url.QueryEscape(info)) resp, err := http.Get(tuLingURL) if err != nil { log.Println(err) return "" } defer resp.Body.Close() reply := new(tlReply) decoder := json.NewDecoder(resp.Body) //decode reply from response body decoder.Decode(reply) return reply.Text}type tlReply struct { code int Text string `json:"text"`}
2. The music is better than the music-share to friends
Through the first step, the robot already has the basic dialogue function, at this time can begin to pull the good friend to flirt together.
Although the go language can compile multiple platform executables for sharing (including but not limited to Linux, Windows, Mac OS), we have many more convenient and elegant ways.
2.1 Public Number
Registering a public number with the developer platform, plus a little bit of code above, allows it to have a conversational function:
2.2 Web Sharing
With some simple front-end technology, you can make conversations more user-friendly:
Online presentation address (voice input can be supported by the browser giving permission)
Here the front and back end uses WebSocket to communicate:
//used by web samaritan robotfunc socketHandler(w http.ResponseWriter, r *http.Request) { c, err := upgrader.Upgrade(w, r, nil) if err != nil { log.Print("upgrade:", err) return } defer c.Close() for { mt, in, err := c.ReadMessage() // read from user input if err != nil { log.Println("read:", err) break } ret := tlAI(string(in)) // get reply from tl AI robot for i := range ret { c.WriteMessage(mt, []byte(ret[i])) time.Sleep(time.Second) } c.WriteMessage(mt, []byte("")) }}
With the push function of WebSocket, the return effect (segment, speed limit) of the conversation becomes controllable.
3. Add Skill Points
Only the conversation function, only called the chat robot, so start to add new features.
Take the simple add memo as an example:
// SaveMemo create a memo for user, saved in redis// command '/memo'func (rb *Robot) SaveMemo(update tgbotapi.Update, step int) (ret string) { user := update.Message.Chat.UserName tmpAction := userAction[user] switch step { case 0: tmpAction.ActionStep++ userAction[user] = tmpAction ret = "Ok, what do you want to save?" case 1: defer delete(userAction, user) when := time.Now().Format("2006-1-02 15:04") memo := update.Message.Text go conn.CreateMemo(user, when, memo) ret = "Ok, type '/memos' to see all your memos" } return}
Interactive mode, the first to determine the user interaction State to give different responses.
Use go conn.CreateMemo(user, when, memo) asynchronous generation of Redis records. :
// CreateMemo saves a memofunc CreateMemo(user, when, memo string) { c := Pool.Get() defer c.Close() var setMemoLua = ` local id = redis.call("INCR", "memoIncrId") redis.call("RPUSH", KEYS[1]..":memos", id) redis.call("HMSET", "memo:"..id, "time", KEYS[2], "content", KEYS[3]) ` script := redis.NewScript(3, setMemoLua) script.Do(c, user, when, memo)}
4. Finally
At this point, the writing of a robot is over, it is interesting to create a small robot from zero to one, and what is really interesting is how big the brain hole is from one to N, and how big the ability of a small robot is.
The code of the text and the small robot code to write their own link: github/evolsnow/robot
If there are other interesting ideas, welcome to the development of play together.
An article to explain the robot automatically find resources, the use of ~: Step by step to teach your robot to find resources link