Advertisement
Guest User

Untitled

a guest
Oct 30th, 2016
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. // Command chatsaver archives a Facebook Messenger chat
  2. // history as a file for github.com/unixpickle/chatbot.
  3. package main
  4.  
  5. import (
  6. "encoding/csv"
  7. "fmt"
  8. "os"
  9. "strings"
  10.  
  11. "github.com/howeyc/gopass"
  12. "github.com/unixpickle/fbmsgr"
  13. )
  14.  
  15. func main() {
  16. fmt.Print("Email/username: ")
  17. user := readLine()
  18. fmt.Print("Password: ")
  19. pass, err := gopass.GetPasswdMasked()
  20. if err != nil {
  21. fmt.Fprintln(os.Stderr, err)
  22. os.Exit(1)
  23. }
  24.  
  25. fmt.Println()
  26. fmt.Println("Authenticating...")
  27.  
  28. sess, err := fbmsgr.Auth(user, string(pass))
  29. if err != nil {
  30. fmt.Fprintln(os.Stderr, "Failed to login:", err)
  31. os.Exit(1)
  32. }
  33.  
  34. fbid := promptChat(sess)
  35.  
  36. fmt.Print("Destination filename: ")
  37. file := readLine()
  38.  
  39. fmt.Println("Downloading messages...")
  40. var outRows [][]string
  41.  
  42. actionChan, errChan := sess.FullActionLog(fbid, nil)
  43. for action := range actionChan {
  44. if msg, ok := action.(*fbmsgr.MessageAction); ok {
  45. if msg.AuthorFBID() == sess.FBID() {
  46. outRows = append(outRows, []string{"bot", msg.Body})
  47. } else {
  48. outRows = append(outRows, []string{"human", msg.Body})
  49. }
  50. fmt.Printf("\rGot %d messages...", len(outRows))
  51. }
  52. }
  53. fmt.Printf("\rTotal of %d messages...\n", len(outRows))
  54.  
  55. if err := <-errChan; err != nil {
  56. fmt.Fprintln(os.Stderr, "Fetch error:", err)
  57. }
  58.  
  59. for i := 0; i < len(outRows)/2; i++ {
  60. outRows[i], outRows[len(outRows)-(i+1)] = outRows[len(outRows)-(i+1)], outRows[i]
  61. }
  62.  
  63. outFile, err := os.Create(file)
  64. if err != nil {
  65. fmt.Fprintln(os.Stderr, "Create output:", err)
  66. os.Exit(1)
  67. }
  68. w := csv.NewWriter(outFile)
  69. err = w.WriteAll(outRows)
  70. outFile.Close()
  71. if err != nil {
  72. fmt.Fprintln(os.Stderr, "Write output:", err)
  73. os.Exit(1)
  74. }
  75. }
  76.  
  77. func promptChat(s *fbmsgr.Session) string {
  78. fmt.Println("Listing your chats...")
  79. var idx int
  80. for {
  81. listing, err := s.Threads(idx, 20)
  82. if err != nil {
  83. fmt.Fprintln(os.Stderr, "Failed to list threads:", err)
  84. os.Exit(1)
  85. }
  86. for _, entry := range listing.Threads {
  87. otherNames := []string{}
  88. for _, id := range entry.Participants {
  89. for _, person := range listing.Participants {
  90. if person.FBID == id {
  91. otherNames = append(otherNames, person.Name)
  92. }
  93. }
  94. }
  95. names := strings.Join(otherNames, ",")
  96. if entry.Name == "" {
  97. fmt.Println(entry.ThreadFBID, "with", names)
  98. } else {
  99. fmt.Printf("%s named %s (with %s)\n", entry.ThreadFBID, entry.Name, names)
  100. }
  101. }
  102. if len(listing.Threads) < 20 {
  103. break
  104. }
  105. idx += len(listing.Threads)
  106. }
  107. fmt.Print("Pick FBID: ")
  108. return readLine()
  109. }
  110.  
  111. func readLine() string {
  112. var res string
  113. for {
  114. buf := make([]byte, 1)
  115. if n, err := os.Stdin.Read(buf); err != nil {
  116. break
  117. } else if n != 0 {
  118. if buf[0] == '\n' {
  119. break
  120. }
  121. res += string(buf[0])
  122. }
  123. }
  124. return res
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement