We are working on a telegram bot. We are attempting to both send a message to users with an expired timestamp and update the timestamp when we send the message. The first part of this is working, and the second is not. I have hardcoded two test ID’s, myself and my coworker are receiving the test messages, but we are not seeing the timestamp updating in DynamoDB. I took the code snippet from a working lambda function. I’ve ensured that there are no problems with permissions (I’ve given full access to dynamodb just to test) and no dice. Could this be an issue with the loop itself somehow? I’ve tried both awaiting a for..of and this for loop with awaiting the functions inside, again only the sendMessage function works. There are no error messages, and only the ID’s with ‘message sent’ actually get logged. Not really sure what the problem could be, or where else to look. Any guidance appreciated.
const TelegramBot = require("node-telegram-bot-api");
const AWS = require("aws-sdk");
AWS.config.region = "eu-west-1";
let docClient, db;
exports.handler = async function (event) {
if (!db) {
db = new AWS.DynamoDB({ region: "eu-west-1" });
}
if (!docClient) {
docClient = new AWS.DynamoDB.DocumentClient({ service: db });
}
let userIds = ["0016027688931", "0016527721826"];
for (let i = 0; i < userIds.length; i++) {
try {
await bot.sendMessage(
parseInt(userIds[i].slice(3)),
'test'
);
await docClient
.update({
TableName: "EXMPLE",
Key: {
USER_ID: "001" + userIds[i],
},
UpdateExpression: "SET LAST_SEEN = :s",
ExpressionAttributeValues: {
":s": Math.floor(Date.now() / 1000),
},
})
.promise();
console.log(userIds[i], " message sent");
} catch (error) {
console.log("Could not send message to ", userIds[i]);
console.log(error.toString());
}
}
};
2
Answers
It was a weird AWS-ism. Deleting and recreating the function with the same parameters worked.
I’m on my phone so I can’t test, but give this a try: