I'm writing a telegram's bot using nestjs-telegraf wrapper. I want to get message text sent by telegram. This is my handler:
#On('text')
async onText(#Ctx() ctx: Context) {
const msg = ctx.message;
const update = ctx.update;
const from = msg.from;
const chat = msg.chat;
const date = msg.date;
// **const text = msg.text;**
// this.logger.debug('Context - ' + JSON.stringify(ctx));
this.logger.debug('update - ' + JSON.stringify(update));
this.logger.debug('text message - ' + JSON.stringify(msg));
this.logger.debug(`message id ${msg.message_id}`);
this.logger.debug(`from id ${from.id}`);
this.logger.debug('from ' + JSON.stringify(from));
this.logger.debug('chat ' + JSON.stringify(chat));
this.logger.debug('date ' + JSON.stringify(date));
// **this.logger.debug('text ' + JSON.stringify(text));**
ctx.reply('You send ' + msg);
}
If I try to build the code removing the comment on line
const text = msg.text;
I got the error: Property 'text' does not exist on type 'New & NonChannel & Message.
compilation error
But, if I run the code with the line commented everithing works and I got this log:
[Nest] 148003 - 31/05/2022, 12:36:28 DEBUG [BotService] text message - {"message_id":361,"from":{...},"chat":{..."},"date":1653992754,"text":"this is a message text"}
where the text property exists and contains the data I sent from telegram. Enough If I run code in debug mode, I can see the object with the text property filled correctly. Can anyone give me some soggest to compile the code and extract the text?
Thanks
running code in debug
Related
I'm trying make my discord.js bot log when people create, and delete channels using the audit logs. I have the create logging down, but the delete part is giving me an error: "UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'name' of undefined". I don't know why it won't work.
My channel delete logging code:
bot.on("channelDelete", async() => {
var channeldeleteauditlogs = await bot.guilds.find(g => g.id === '621810400924794890').fetchAuditLogs({
type: 11
});
let entry = channeldeleteauditlogs.entries.first();
var channeldeletelog = fs.readFileSync('channeldeletelog.txt', 'utf-8');
var channeldeletelog2 = fs.writeFileSync('channeldeletelog.txt', '\nTime - ' + entry.createdAt.toUTCString() + ' | Channel - ' + entry.target.name + ' | User - ' + entry.executor.username + ' (' + entry.executor.id + ')' + channeldeletelog);
console.log(entry.executor.username + " deleted a channel");
});
My (Successful) channel create logging code:
bot.on("channelCreate", async() => {
let channelcreateauditlogs = await bot.guilds.find(g => g.id === '621810400924794890').fetchAuditLogs({
type: 10
});
let entry = channelcreateauditlogs.entries.first();
var channelcreatelog = fs.readFileSync('channelcreatelog.txt', 'utf-8');
var channelcreatelog2 = fs.writeFileSync('channelcreatelog.txt', '\nTime - ' + entry.createdAt.toUTCString() + ' | Channel - ' + entry.target.name + ' (' + entry.target.id + ') | User - ' + entry.executor.username + ' (' + entry.executor.id + ')' + channelcreatelog);
console.log(entry.executor.username + " created a channel");
});
Thanks.
You can try using the channel parameter that comes with the channelDelete event.
bot.on("channelDelete", async (channel) => {
console.log(channel.name)
I'm setting up a Twitter bot to tweet out a city's temperature, I got the API but I can't seem to hook it on my bot.js file
I tried changing the variables but nothing seems to work.
var Twit = require('twit');
var config = require('./config');
var T = new Twit(config);
gotData();
function setup() {
loadJSON("http://api.apixu.com/v1/current.json?key=7165704df08340e9b00213540192507&q=Colombo", gotData);
}
function gotData(data) {
console.log('Weather Data Retrieved...')
var r = data.current[2];
var tweet = {
status: 'here is ' + r + ' temperature test '
}
T.post('statuses/update', tweet);
}
TypeError: Cannot read property 'current' of undefined
Your call to gotData() does not pass a data argument so when the gotData function attempts to access data.current[2] data is undefined. By the looks of your code you just need to change the line gotData(); to setup();
I want send Excel with Angular to my Server (SpringBoot). I can't do this.
Error 500 in server
So, I thought that my problem maybe is "Angular doesn't read Excel." I am trying read my Excel with Angular and I get error ->
My intention is "If Angular can read the Excel, then I will try send Excel."
Because I am trying send Excel and I can't send...
Then:
In this code:
fileChange(event) {
let file: File = event.files[0];
let myReader: FileReader = new FileReader();
let fileType = event.parentElement.id;
myReader.onloadend = function (e) {
console.log(myReader.result);
}
console.log('fileType' , fileType);
myReader.readAsText(file);
}
ERROR
ERROR TypeError: Cannot read property '0' of undefined
This code I got the ->
Upload a File and Read Data with FileReader in Angular 2
Before I try with this code:
fileChange(event) {
let fileList: FileList = event.target.files;
if (fileList.length > 0) {
let file: File = fileList[0];
this.formData.append('uploadFile', file, file.name);
console.log('al cambiar file' , file);
}
console.log(' formdata' , this.formData );
console.log(' fileList' , fileList);
console.log('stack' , event.files[0]);
}
result
With this code I get name the File and last modification but I can't get cells of Excel.
use 'xlsx' package.
npm install xlsx
I've been working on creating my own home automation hub using a handful of node.js packages (Express, mqtt, socket.io) with a MongoDB database and Angular running in the client. This project is the first time I've worked with any JavaScript, so it's safe to say I'm a bit of a noob.
I've been troubleshooting an issue with derived EventEmitters calling the wrong listener function when referenced as a named property. Here's a minimal example that will demonstrate the problem:
var EventEmitter = require('events').EventEmitter;
var debug = require('debug')('test.js');
var util = require('util');
var TestEmitter = function (initialState, name) {
var self = this;
EventEmitter.call(self);
this.state = initialState;
this.name = name;
this.setState = function (newState) {
self.emit('change', newState, this.state);
self.state = newState;
};
};
util.inherits(TestEmitter, EventEmitter);
var myObj = {
ary: [new TestEmitter(false, 'ary1'), new TestEmitter(true, 'ary2')],
named: {
name1: new TestEmitter(3, 'name1'),
name2: new TestEmitter(4, 'name2')
}
};
myObj.ary.forEach(function (aryEmitter) {
aryEmitter.on('change', function (newState) {
debug(aryEmitter.name + ' changed to: ' + newState);
});
});
for (var prop in myObj.named) {
var currEmitter = myObj.named[prop];
debug('prop = ' + prop);
debug('name = ' + currEmitter.name);
currEmitter.on('change', function (newState) {
debug(currEmitter.name + ' changed to: ' + newState);
});
}
myObj.ary[0].setState(true);
myObj.ary[1].setState(false);
myObj.named.name1.setState(4);
myObj.named.name2.setState(5);
Essentially, TestEmitter is an object derived from EventEmitter that will broadcast a change event when its setState method is called.
myObj is a reference to four TestEmitter instances -- two in an array, and two in named properties. After creating myObj, I register a listener for each of their change events that simply writes debugging output to the console with the name of the TestEmitter instance whose callback is being invoked.
However, the named TestEmitter references don't work the way that I would expect. The call to both myObj.named.name1.setState(4) as well as myObj.named.name2.setState(5) will both execute the callback function that I registered for the EventEmitter myObj.named.name2. Running everything above will produce the following output:
test.js prop = name1 +0ms
test.js name = name1 +5ms
test.js prop = name2 +0ms
test.js name = name2 +0ms
test.js ary1 changed to: true +0ms
test.js ary2 changed to: false +0ms
test.js name2 changed to: 4 +0ms
test.js name2 changed to: 5 +0ms
Can anyone offer any help? I've read a fair amount about the best way to create derived EventEmitters and it looks like I'm taking the right approach, so I'm a bit stumped.
Thanks for reading and any help you're able to offer!
Your problem becomes at this part of the code:
for (var prop in myObj.named) {
var currEmitter = myObj.named[prop];
debug('prop = ' + prop);
debug('name = ' + currEmitter.name);
currEmitter.on('change', function (newState) {
debug(currEmitter.name + ' changed to: ' + newState);
});
}
So that when an event is fired, inside the function handler, currEmitter has the value of the last element of loop.
One simple fix is to use encapsule it in a function, to create a new scope, and do the things inside this function :
for (var prop in myObj.named) {
var currEmitter = myObj.named[prop];
(function(currEmitter){
currEmitter.on('change', newState=>{
debug(currEmitter.name + ' changed to: ' + newState);
});
})(currEmitter);
}
Or you can use Object.keys():
Object.keys(myObj.named).forEach(function(key){
var currEmitter = myObj.named[key];
currEmitter.on('change', function(newState){
debug(currEmitter.name + ' changed to: ' + newState);
});
});
i am using https://github.com/gf3/sandbox#readme but i am not getting that how can i pass some data from my code to javascript code through this sandbox. for example
var s = new sandBox();
s.run("(function(name) { return 'Hi there, ' + name + '!'; })('Fabio')", function(output) {
console.log("Example 2: " + output.result + "\n")
})
now i want to pass some data to this function how can i do that?
It does not have any clean way to pass arguments. However, since you are passing code as a string anyway, you can simply add the arguments directly to the string:
var arg = 'Hello';
var code = "(function(name) { return 'Hi there, ' + name + '!'; })("+ JSON.stringify(arg) +")"
s.run(code, ...);
I'm using JSON.stringify to ensure that the string is a valid JS expression.