Opta and Telegram

I would like to use the finder opta to send messages via Telegram, Whatsapp, and email.

So far, I have done some research regarding Telegram, but it seems that all the available libraries are for ESP32 and ESP8266.

Is there a system to send Telegram messages through opta in a simple way?

Since the Opta relies on the standard Arduino Ethernet/WiFi libraries for connectivity, it should work without problems with those libraries that use a generic Client like this (the second one is mine):

But the most important thing is that your client must be able to establish an SSL connection, because the Telegram server only accepts connections of that type on port 443

I have conducted tests with AsyncTelegram2, but I always encounter compilation errors because Opta has a different chip than ESP32 and ESP8266.

Is it possible to use the AsyncTelegram2 library if the chip is different from ESP32 or ESP8266?

This sounds me strange.
Let me do dome "compile" tests (unfortunately I don't have any options to do real tests).

1 Like

Ok , after I will try to make an easy example and I share it here.

I can compile without errors this simple sketch, but as I said before, the client must handle SSL connection and the Arduino WiFi library doesn't support SSL.

So additional SSLClient library is needed (you will have to be patient because the compilation times take quite a bit).

/*
  Name:        echoBot.ino
  Created:     26/03/2021
  Author:      Tolentino Cotesta <cotestatnt@yahoo.com>
  Description: a simple example that check for incoming messages
               and reply the sender with the received message.
                 The message will be forwarded also in a public channel
                 anad to a specific userid.
*/

#include <AsyncTelegram2.h>
#include <SSLClient.h>
#include <tg_certificate.h>

// Timezone definition
#include <time.h>
#define MYTZ "CET-1CEST,M3.5.0,M10.5.0/3"

#include <WiFi.h>

WiFiClient base_client;
int status = WL_IDLE_STATUS;

SSLClient client(base_client, TAs, (size_t)TAs_NUM, A0, 1, SSLClient::SSL_ERROR );
AsyncTelegram2 myBot(client);

const char* ssid  =  "xxxxxxxxx";     // SSID WiFi network
const char* pass  =  "xxxxxxxxx";     // Password  WiFi network
const char* token =  "xxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";  // Telegram token

// Target user can find it's own userid with the bot @JsonDumpBot
// https://t.me/JsonDumpBot
int64_t userid = 123456789;  

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  // initialize the Serial
  Serial.begin(115200);
  Serial.println("\nStarting TelegramBot...");

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 3 seconds for connection:
    delay(3000);
  }
  Serial.println("Connected to wifi");  
  
  // Set the Telegram bot properies
  myBot.setUpdateTime(2000);
  myBot.setTelegramToken(token);

  // Check if all things are ok
  Serial.print("\nTest Telegram connection... ");
  myBot.begin() ? Serial.println("OK") : Serial.println("NOK");

  char welcome_msg[128];
  snprintf(welcome_msg, 128, "BOT @%s online\n/help all commands avalaible.", myBot.getBotName());

  // Send a message to specific user who has started your bot
  myBot.sendTo(userid, welcome_msg);
}

void loop() {
  
  static uint32_t ledTime = millis();
  if (millis() - ledTime > 150) {
    ledTime = millis();
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  }

  // local variable to store telegram message data
  TBMessage msg;

  // if there is an incoming message...
  if (myBot.getNewMessage(msg)) {    
    // Send a message to your public channel
    String message ;
    message += "Message from @";
    message += myBot.getBotName();
    message += ":\n";
    message += msg.text;
    Serial.println(message);    

    // echo the received message
    myBot.sendMessage(msg, msg.text);
  }
}

Thank you for the response!!
Tonight I'll try, but I currently connect the board via WiFi using the ArduinoIoTCloud and Arduino_ConnectionHandler libraries.

By using the WiFi.h library, can I still leverage the Arduino cloud?

Arduino_ConnectionHandler is build on top of WiFi.h library, so it should be enough using an additional instance of WiFiClient.

I tried to compile the sketch you shared, and it compiles perfectly without errors. However, when I add the SSLClient.h library to my project, I encounter compilation errors that I can't decipher. Essentially, the combination of these libraries generates a compilation error.

#include <SSLClient.h>
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

Digging into ArduinoIoTCloud library, with Opta board, seems already included the bearssl framework and I suppose it's not compatible with ClientSSL togheter.

But this means that you have support for SSL connection through the ArduinoIoTCloud dependencies and this is good because you can use another BearSSLClient client also with Telegram.

The question now is, how can you use the Telegram SSL certificate with this client?
Looking at the source of BearSSLClient, the constructor is written in this manner:

BearSSLClient(Client* client, const br_x509_trust_anchor* myTAs, int myNumTAs, GetTimeCallbackFunc func);

It's very similar to ClientSSL indeed.

I've tried this code and compiled with success, try and let us know it it works finally!

#include <WiFi.h>
#include <AsyncTelegram2.h>
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
#include <tg_certificate.h>

WiFiClient base_client;
int status = WL_IDLE_STATUS;

unsigned long myGetTime() {
  return ArduinoCloud.getInternalTime();
}

BearSSLClient client(&base_client, TAs, (size_t)TAs_NUM, myGetTime);
AsyncTelegram2 myBot(client);

..... the rest is the same

Hi, thank you for the suggestions; I'm doing some tests.

I found the ArduinoIoTPreferredConnection.getClient() instruction, and it's possible to pass it to BearSSLClient.

I've tried both the code you suggested and ArduinoIoTPreferredConnection.getClient(), but when I execute myBot.begin(), I receive the response "Unable to connect to Telegram server," and the connection with the Arduino cloud also drops: ArduinoIoTCloudTCP::handle_Disconnect MQTT client connection lost and Board disconnected from Arduino IoT Cloud.