Facebrick Chatbot (pt2)
Music: Snake Eyes (feat. CoMa) - Feint (Any ads are their's)
Overview
Decided on building a Facebook bot from scratch. Here's the first timelapse video (starting at part two because the first video building the POC contains some sensitive details). Anyway, this is just to test. Enjoy the second day of building "Facebrick Chatbot".
Code thus far
File: clsFBMessengerMonitor.vb
Imports System.ComponentModel
Imports System.Net
Namespace LoKI.AceldamA
Public Class FBMessengerMonitor
'-- Consts
Private Const USERAGENTSTRING As String = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0"
'-- Globals
Private _Timer As BackgroundWorker
Private _TimerRunning As Boolean = False
Private _LastScrape() As FBMessageMain
Private _HttpCookies As CookieContainer = New CookieContainer()
'-- Structures and Enums
Public Enum ErrorCode
TimerAlreadyRunning
TimerAlreadyStopping
TimerNotRunning
End Enum
Public Structure FBMessageMain
Public Property Name As String
Public Property Read As Boolean
Public Property Link As String
Public Property Peek As String
Public Sub Parse(RawHTML As String)
'-- To Do
End Sub
Public Overrides Function ToString() As String
Return String.Format("[{0}] {1} '{2}' ({3})", IIf(Read, "_", "X"), Name, Peek, Link)
End Function
End Structure
Private Structure HtmlForm
Dim FormKVPs As Dictionary(Of String, String)
Public Property Action As String
Default Public Property Key(KeyName) As String
Get
If FormKVPs.ContainsKey(KeyName) Then
Return FormKVPs(KeyName)
Else
Return Nothing
End If
End Get
Set(value As String)
Me.FormKVPs.Add(KeyName, value)
End Set
End Property
Public Sub Parse(OuterHTML As String)
'-- To Do
End Sub
Public Function PostData() As String
'-- To Do
Return ""
End Function
End Structure
'-- Events
Public Event MessagesChanged(NewMessages() As FBMessageMain, OldMessages() As FBMessageMain)
Public Event [Error](Code As ErrorCode)
'-- Properties
Public Property TimerInterval As Integer = 10000
'-- Constructor
Public Sub New(Username As String, Password As String)
'-- Attempt Login
Me._Login(Username, Password)
'-- Set up the timer backgroundworker
Me._Timer = New BackgroundWorker
AddHandler Me._Timer.DoWork, AddressOf Me._TimerStart
End Sub
#Region "Timer"
Private Delegate Sub TickDelegate()
Private Sub _TimerTick()
End Sub
Private Sub _TimerStart(sender As BackgroundWorker, e As DoWorkEventArgs)
Dim I As Integer = Me.TimerInterval
Me._TimerRunning = True
Do While Me._TimerRunning
'-- Waiting loop
Do While Me._TimerRunning AndAlso (I < Me.TimerInterval)
System.Threading.Thread.Sleep(100)
I = I + 100
Loop
I = 0
'-- Worker part of the timer
If Me._TimerRunning Then
'-- Fire threadsafe "OnTimerTick" event handler
Windows.Application.Current.Dispatcher.Invoke(
Threading.DispatcherPriority.Normal,
New TickDelegate(AddressOf Me._TimerTick)
)
End If
Loop
End Sub
Public Sub StartMonitor()
If Not Me._Timer.IsBusy Then
Me._Timer.RunWorkerAsync()
Else
'-- Raise error
RaiseEvent Error(ErrorCode.TimerAlreadyRunning)
End If
End Sub
Public Sub StopMonitor()
If Me._TimerRunning Then
Me._TimerRunning = False
ElseIf Me._Timer.IsBusy Then
RaiseEvent Error(ErrorCode.TimerAlreadyStopping)
Else
RaiseEvent Error(ErrorCode.TimerNotRunning)
End If
End Sub
#End Region
#Region "HTTP"
Private Function _UrlEncode(Text As String) As String
Dim tStr As String = ""
For Each c As Char In Text
If "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_+-.".Contains(c) Then
tStr = String.Format("{0}{1}", tStr, c)
Else
Select Case c
Case Is = " "
tStr = String.Format("{0}+", tStr)
Case Else
tStr = String.Format("{0}%{1:x2}", tStr, Asc(c))
End Select
End If
Next
Return tStr
End Function
Private Function _UrlDecode(Text As String) As String
Dim tSplit() As String = Split(Text, "%")
Dim tStr As String = tSplit(0)
For I As Integer = 1 To UBound(tSplit)
tStr = String.Format(
"{0}{1}{2}",
tStr,
Chr(CInt(String.Format("&H{0}", Mid(tStr, 1, 2)))),
Mid(tStr, 3)
)
Next
Return tStr
End Function
Private Function _GetForms(RawHtml As String) As HtmlForm()
'-- To Do
Return Nothing
End Function
Private Function _Http(ByVal Url As String, Referer As String, Optional PostData As String = Nothing) As String
Dim Request As HttpWebRequest = DirectCast(HttpWebRequest.Create(Url), HttpWebRequest)
Dim Response As HttpWebResponse
Dim HttpPostData As Char() = Nothing
Dim ResponseString As String = ""
With Request
.AllowAutoRedirect = True
If PostData Is Nothing Then
.Method = WebRequestMethods.Http.Get
Else
'-- POST data was supplied
HttpPostData = Text.Encoding.UTF8.GetChars(Text.Encoding.UTF8.GetBytes(PostData))
.Method = WebRequestMethods.Http.Post
.ContentType = "application/x-www-form-urlencoded"
.ContentLength = HttpPostData.Length
End If
.Host = "m.facebook.com"
.UserAgent = USERAGENTSTRING
.Headers.Add(HttpRequestHeader.AcceptEncoding, "text/html, application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
.Headers.Add(HttpRequestHeader.AcceptLanguage, "en,en-GB;q=0.5")
.Referer = Referer
.CookieContainer = Me._HttpCookies
.KeepAlive = True
.Headers.Add(HttpRequestHeader.CacheControl, "max-age=0")
End With
If Not PostData Is Nothing Then
'-- Perform the POST first
Using sw As New System.IO.StreamWriter(Request.GetRequestStream)
sw.Write(HttpPostData, 0, HttpPostData.Length)
sw.Close()
End Using
End If
'-- Read the HTTP response
Response = Request.GetResponse()
Using sr As New System.IO.StreamReader(Response.GetResponseStream())
ResponseString = sr.ReadToEnd()
sr.Close()
End Using
'-- Return the HTTP response
Return ResponseString
End Function
#End Region
'-- Facebrick Methods
Private Sub _Login(User As String, Pass As String)
'-- To Do
End Sub
End Class
Public Class FacebrickDeets
Public ReadOnly Property Username As String = "YOUR USERNAME"
Public ReadOnly Property Password As String = "YOUR PASSWORD"
End Class
End Namespace
@aceldama, welcome and congratulations on making your first post! I gave you a $.02 vote! If you would be so kind to give me a follow in return that would be awesome!
done and done. thank you introbot! beep boop
Congratulations @aceldama! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
You published your First Post
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP
Congratulations @aceldama! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
You made your First Comment
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP