我正在努力改善自己的机器人,以欢迎将加入群组的每个用户。 但是我找不到解决办法。命令运行正常,但是关于欢迎消息,我陷入了困境,找不到任何解决方案,无处可解决此问题...
我试图查找具有此功能的另一个bot代码,不幸的是,我发现的所有内容都是用python语言编写的bot ...
这是下面的代码...
'use strict';
const Telegram = require('telegram-node-bot'),
tg = new Telegram.Telegram('MYAPIKEY', {
workers: 1
});
//Commands for bot, codes of the commands are in the controllers
const InfoController = require('./controllers/info')
, RulesController = require('./controllers/rules')
, PriceController = require('./controllers/price')
tg.router.when(new Telegram.TelegramTextCommand('/info', 'infoCommannd'), new InfoController())
tg.router.when(new Telegram.TelegramTextCommand('/rules', 'rulesCommannd'), new RulesController())
tg.router.when(new Telegram.TelegramTextCommand('/price', 'priceCommannd'), new PriceController());
//Welcome Message in the chatgroup to new user that joins group
function telegram_user() {
this.telegram_id = 0;
this.telegram_username = null;
this.telegram_firstname = null;
this.telegram_lastname = null;
this.some_array = [];
this.some_other_array = []; //yes you can store arrays in objects
this.store_some_message = null;
}
//initialise global user array
var user_array = [];
user_array.push(new telegram_user()); //i just like to fill the [0] with a blank user
function auth_usr(telegram_id) {
for(var i = 0; i < user_array.length; i++)
{
if (user_array[i].telegram_id === telegram_id) {
return i; //return the user's position in the user_array
}
}
return 0; //otherwise just return 0;
}
//instantiate new user state object
if(auth_usr(message.from.id) === 0)
{
user_array.push(new telegram_user());
user_array[user_array.length-1].telegram_id = message.from.id;
user_array[user_array.length-1].telegram_username = message.from.username;
user_array[user_array.length-1].telegram_firstname = message.from.first_name;
user_array[user_array.length-1].telegram_lastname = message.from.last_name;
telegram.sendMessage(message.chat.id, "Welcome" + telegram_firstname + "! Do not forget to read rules with /rules command" , {parse_mode: "Markdown"}); //When user joins it will Say "Welcome "first name" "! Do not forget...!"
return;
}
代码有问题,无法修复 编译index.js时出错
if(auth_usr(message.from.id) === 0)
^
ReferenceError: message is not defined
我的预期结果是当新用户加入该组时,此消息将出现: “欢迎鲍勃!别忘了用/ rules命令阅读规则。”