B4R Question Example or Library for Telegram/WhatsApp Notifications using ESP8266 in B4R?

Cesar_Morisco

Active Member
Licensed User
Longtime User
Hi everyone,

I'm working on a project using B4R with an ESP8266, and I would like to send notifications through Telegram or WhatsApp. Does anyone have an example or know if there's a library available for this purpose? Any help, example code, or suggestions would be greatly appreciated.

Thanks in advance!
 

peacemaker

Expert
Licensed User
Longtime User
I use ESP8266 sensors sending data to a PHP-based web-server hosting the Telegram-bot code for alerts.
But it's very huge system, MCU firmware is simplest one here.
 
Upvote 0

Cesar_Morisco

Active Member
Licensed User
Longtime User
Hello everyone
I did a little test here and I don't think I'm on the right path. Could someone help me get to the rocks?
B4R:
Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Private socket As WiFiSocket
    Private astream As AsyncStreams
    Private bc As ByteConverter
    Private eol() As Byte = Array As Byte(13, 10)
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("Iniciando...")

    If wifi.Connect2("........", ".......") Then
        Log("Wi-Fi conectado.")
    Else
        Log("Falha ao conectar no Wi-Fi.")
        Return
    End If
    
    If socket.ConnectHost("api.telegram.org", 80) Then
        Log("Conectado ao Telegram.")
        astream.Initialize(socket.Stream, "Astream_NewData", "Astream_Error")
        EnviarTelegram("Teste do ESP8266 via B4R")
    Else
        Log("Erro ao conectar ao Telegram.")
    End If

End Sub

Sub EnviarTelegram(mensagem As String)
    Dim token As String = "01080098709:AAFA-25RJQpCboLL3h9qlAtBL8R40MrFHQA" 
    Dim chatID As String = "1192822222"
    Dim mensagem As String = "Mensagem enviada pelo ESP8266 via B4R"

    Dim linha1 As String = JoinStrings(Array As String("GET /bot", token, "/sendMessage?chat_id=", chatID, "&text=", mensagem, " HTTP/1.1"))
    Dim linha2 As String = "Host: api.telegram.org"
    Dim linha3 As String = "Connection: close"

    astream.Write(bc.StringToBytes(linha1))
    astream.Write(eol)
    astream.Write(bc.StringToBytes(linha2))
    astream.Write(eol)
    astream.Write(bc.StringToBytes(linha3))
    astream.Write(eol)
    astream.Write(eol) ' linha em branco obrigatória para finalizar o cabeçalho HTTP
End Sub

Sub Astream_NewData(Buffer() As Byte)
    Log("Resposta:")
    Log(bc.StringFromBytes(Buffer))
End Sub

Sub Astream_Error
    Log("Erro na conexão do stream.")
End Sub
 
Upvote 0