mirror of
				https://github.com/matrix-org/gomatrix
				synced 2025-11-03 22:08:04 +00:00 
			
		
		
		
	Merge 9d22d0bb41 into 1ed3413986
				
					
				
			This commit is contained in:
		
						commit
						524157d9c4
					
				
					 3 changed files with 128 additions and 2 deletions
				
			
		| 
						 | 
					@ -515,7 +515,7 @@ func (cli *Client) ForgetRoom(roomID string) (resp *RespForgetRoom, err error) {
 | 
				
			||||||
// InviteUser invites a user to a room. See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-invite
 | 
					// InviteUser invites a user to a room. See http://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-rooms-roomid-invite
 | 
				
			||||||
func (cli *Client) InviteUser(roomID string, req *ReqInviteUser) (resp *RespInviteUser, err error) {
 | 
					func (cli *Client) InviteUser(roomID string, req *ReqInviteUser) (resp *RespInviteUser, err error) {
 | 
				
			||||||
	u := cli.BuildURL("rooms", roomID, "invite")
 | 
						u := cli.BuildURL("rooms", roomID, "invite")
 | 
				
			||||||
	_, err = cli.MakeRequest("POST", u, struct{}{}, &resp)
 | 
						_, err = cli.MakeRequest("POST", u, req, &resp)
 | 
				
			||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,7 +10,7 @@ type Event struct {
 | 
				
			||||||
	StateKey  *string                `json:"state_key,omitempty"` // The state key for the event. Only present on State Events.
 | 
						StateKey  *string                `json:"state_key,omitempty"` // The state key for the event. Only present on State Events.
 | 
				
			||||||
	Sender    string                 `json:"sender"`              // The user ID of the sender of the event
 | 
						Sender    string                 `json:"sender"`              // The user ID of the sender of the event
 | 
				
			||||||
	Type      string                 `json:"type"`                // The event type
 | 
						Type      string                 `json:"type"`                // The event type
 | 
				
			||||||
	Timestamp int                    `json:"origin_server_ts"`    // The unix timestamp when this message was sent by the origin server
 | 
						Timestamp int64                  `json:"origin_server_ts"`    // The unix timestamp when this message was sent by the origin server
 | 
				
			||||||
	ID        string                 `json:"event_id"`            // The unique ID of this event
 | 
						ID        string                 `json:"event_id"`            // The unique ID of this event
 | 
				
			||||||
	RoomID    string                 `json:"room_id"`             // The room the event was sent to. May be nil (e.g. for presence)
 | 
						RoomID    string                 `json:"room_id"`             // The room the event was sent to. May be nil (e.g. for presence)
 | 
				
			||||||
	Content   map[string]interface{} `json:"content"`             // The JSON content of the event.
 | 
						Content   map[string]interface{} `json:"content"`             // The JSON content of the event.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										126
									
								
								example/example.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										126
									
								
								example/example.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,126 @@
 | 
				
			||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//import "github.com/kr/pretty"
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
 | 
						"github.com/donomii/gomatrix"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Login to a local homeserver and set the user ID and access token on success.
 | 
				
			||||||
 | 
					func Login(server, username, password string) *gomatrix.Client {
 | 
				
			||||||
 | 
						cli, _ := gomatrix.NewClient(server, "", "")
 | 
				
			||||||
 | 
						resp, err := cli.Login(&gomatrix.ReqLogin{
 | 
				
			||||||
 | 
							Type:     "m.login.password",
 | 
				
			||||||
 | 
							User:     username,
 | 
				
			||||||
 | 
							Password: password,
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							panic(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						cli.SetCredentials(resp.UserID, resp.AccessToken)
 | 
				
			||||||
 | 
						return cli
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func ProcessResponse(resSync *gomatrix.RespSync) {
 | 
				
			||||||
 | 
						for _,v := range resSync.Rooms.Join {
 | 
				
			||||||
 | 
							//fmt.Printf("%v: %# v\n\n\n", k, pretty.Formatter(v.Timeline.Events))
 | 
				
			||||||
 | 
							for _, e := range v.Timeline.Events {
 | 
				
			||||||
 | 
								if e.Type ==  "m.room.message" {
 | 
				
			||||||
 | 
									fmt.Printf("%v: %v\n", e.Sender, e.Content["body"])
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func ExtractRooms(resSync *gomatrix.RespSync) map[string]string{
 | 
				
			||||||
 | 
						rooms := map[string]string{}
 | 
				
			||||||
 | 
						for roomID,v := range resSync.Rooms.Join {
 | 
				
			||||||
 | 
							//fmt.Printf("%v: %# v\n\n\n", k, pretty.Formatter(v.Timeline.Events))
 | 
				
			||||||
 | 
							for _, e := range v.Timeline.Events {
 | 
				
			||||||
 | 
								if e.Type ==  "m.room.name" {
 | 
				
			||||||
 | 
									rooms[fmt.Sprint(e.Content["name"])] = roomID
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return rooms
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func main () {
 | 
				
			||||||
 | 
						log.Print("Logging in...")
 | 
				
			||||||
 | 
						username := os.Args[2]
 | 
				
			||||||
 | 
						inviteUser := os.Args[4]
 | 
				
			||||||
 | 
						roomname := "TestRoomFor" + username
 | 
				
			||||||
 | 
						roomalias := roomname
 | 
				
			||||||
 | 
						cli := Login(os.Args[1], username, os.Args[3])
 | 
				
			||||||
 | 
						log.Println("Done!")
 | 
				
			||||||
 | 
						cli.SetDisplayName(os.Args[2])
 | 
				
			||||||
 | 
						log.Print("Creating room...")
 | 
				
			||||||
 | 
						resp, err := cli.CreateRoom(&gomatrix.ReqCreateRoom{
 | 
				
			||||||
 | 
						RoomAliasName: roomalias,
 | 
				
			||||||
 | 
						Name : roomname,
 | 
				
			||||||
 | 
						Preset: "public_chat",
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
 | 
						id := "!JHMwXpSMhcSnyAGDWB:matrix.org"
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Println(err)
 | 
				
			||||||
 | 
							log.Println("Failed!")
 | 
				
			||||||
 | 
							id = "#" + roomalias + ":matrix.org"
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							log.Println("Done!")
 | 
				
			||||||
 | 
							fmt.Println("Room:", resp.RoomID)
 | 
				
			||||||
 | 
							id =  resp.RoomID
 | 
				
			||||||
 | 
							log.Print("Joining room...")
 | 
				
			||||||
 | 
							if _, err := cli.JoinRoom(id, "", nil); err != nil {
 | 
				
			||||||
 | 
								panic(err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							log.Println("Done!")
 | 
				
			||||||
 | 
							fmt.Println("Room:", id)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						log.Print("Syncing...")
 | 
				
			||||||
 | 
						filter := `{"room":{"timeline":{"limit":50}}}`
 | 
				
			||||||
 | 
						resSync, err := cli.SyncRequest(5, "", filter, false, "")
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							panic(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						ProcessResponse(resSync)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						nextbatch := resSync.NextBatch
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						//get room id
 | 
				
			||||||
 | 
						rooms := ExtractRooms(resSync)
 | 
				
			||||||
 | 
						log.Println(rooms)
 | 
				
			||||||
 | 
						id = rooms[roomname]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						log.Print("Sending message...")
 | 
				
			||||||
 | 
						if _, err := cli.SendText(id, "Hello") ; err != nil {
 | 
				
			||||||
 | 
							panic(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						log.Println("Done!")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						log.Print("Inviting user...")
 | 
				
			||||||
 | 
						if _, err := cli.InviteUser(id, &gomatrix.ReqInviteUser{UserID: inviteUser}) ; err != nil {
 | 
				
			||||||
 | 
							log.Println(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						log.Println("Done!")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for {
 | 
				
			||||||
 | 
							resSync, err := cli.SyncRequest(5, nextbatch, filter, false, "")
 | 
				
			||||||
 | 
							ProcessResponse(resSync)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								panic(err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							nextbatch = resSync.NextBatch
 | 
				
			||||||
 | 
							time.Sleep(2 * time.Second)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						log.Println("Done!")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue