Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functions for chatDescription #210

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@
.gcc-flags.json
.pio/
.vscode/

# MacOS
.DS_Store
95 changes: 95 additions & 0 deletions src/UniversalTelegramBot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,101 @@ bool UniversalTelegramBot::sendChatAction(const String& chat_id, const String& t
return sent;
}


bool UniversalTelegramBot::getChatDescription(String chat_id, String & text)
{
String command = "bot" + _token + "/getChat?chat_id=" + chat_id;
String response = sendGetToTelegram(command); // receive reply from telegram.org
DynamicJsonDocument doc(maxMessageLength);
DeserializationError error = deserializeJson(doc, response);
JsonObject obj = doc.as<JsonObject>(); //there is nothing better right now to use obj.containsKey("result")
closeClient();

if (!error)
{
if (obj.containsKey("result"))
{
String descr = doc["result"]["description"];
text = descr;
return true;
}
}

return false;
}



bool UniversalTelegramBot::setChatDescription(String chat_id, String text)
{
bool sent = false;
#ifdef TELEGRAM_DEBUG
Serial.println(F("SEND Chat Description"));
#endif
long sttime = millis();

if (text != "")
{
while (millis() < sttime + 8000)
{ // loop for a while to send the message
String command = "bot" + _token + "/setChatDescription?chat_id=" + chat_id + "&description=" + text;
String response = sendGetToTelegram(command);

#ifdef TELEGRAM_DEBUG
Serial.println(response);
#endif
sent = checkForOkResponse(response);

if (sent) break;
}
}

closeClient();
return sent;
}



bool UniversalTelegramBot::restrictChatMember(String chat_id, String user_id, bool permit, String until_date)
{
bool sent = false;
long sttime = millis();

DynamicJsonDocument doc(maxMessageLength);
doc["chat_id"] = chat_id;
doc["user_id"] = user_id;
JsonObject permissions = doc.createNestedObject("permissions");
permissions["can_send_messages"] = permit ? "True" : "False";
doc["until_date"] = until_date;
// DeserializationError error = deserializeJson(doc, response);
JsonObject payload = doc.as<JsonObject>(); //there is nothing better right now to use obj.containsKey("result")

#ifdef TELEGRAM_DEBUG
Serial.print(F("SEND User Restriction"));
serializeJson(payload, Serial);
Serial.println();
#endif

if (payload.containsKey("text"))
{
while (millis() < sttime + 8000)
{ // loop for a while to send the message
String command = "bot" + _token + "/restrictChatMember";
String response = sendPostToTelegram(command, payload);
#ifdef TELEGRAM_DEBUG
Serial.println(response);
#endif
sent = checkForOkResponse(response);
if (sent) break;
}
}

closeClient();
return sent;
}



void UniversalTelegramBot::closeClient() {
if (client->connected()) {
#ifdef TELEGRAM_DEBUG
Expand Down
6 changes: 5 additions & 1 deletion src/UniversalTelegramBot.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class UniversalTelegramBot {

bool readHTTPAnswer(String &body, String &headers);
bool getMe();
String GetName() { return name; }

bool sendSimpleMessage(const String& chat_id, const String& text, const String& parse_mode);
bool sendMessage(const String& chat_id, const String& text, const String& parse_mode = "");
Expand Down Expand Up @@ -115,6 +116,9 @@ class UniversalTelegramBot {

String buildCommand(const String& cmd);

bool getChatDescription(String chat_id, String & text);
bool setChatDescription(String chat_id, String text);
bool restrictChatMember(String chat_id, String user_id, bool permit, String until_date);
int getUpdates(long offset);
bool checkForOkResponse(const String& response);
telegramMessage messages[HANDLE_MESSAGES];
Expand All @@ -125,7 +129,7 @@ class UniversalTelegramBot {
unsigned int waitForResponse = 1500;
int _lastError;
int last_sent_message_id = 0;
int maxMessageLength = 1500;
int maxMessageLength = 10500;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned about the impact this will have on memory, does it need to be so large?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, this is certainly a point of discussion and this is probably application dependent. I created a chat bot using the 1500 length setting and found that it did not work at all because messages were incomplete rendering the reply useless. In effect he bot got stuck doing nothing so that I thought my code was bad. Debugging into it gave me the hint to modify the message size.

I suggest making this an obvious parameter for the library by moving it to private and adding at least a set method to make clear it is meant for application management. In addition, it could become a parameter of the contructor if you don't mind.

If you want to go ahead with this change, I can offer to make the necessary modification.


private:
// JsonObject * parseUpdates(String response);
Expand Down