How do I handle user response (facebook) chat bot? - node.js

Trying to create a facebook chatbot with NodeJS.
The problem is that I can easily handle user response to "button" messages with few choices to pick but can't handle input from simple text messages ("what is your favourite color?").
The conversation should be as follows :
Bot : Welcome, I have some questions for you.
What is your phone number?
User :
Bot : How old are you?
User :
etc.
So what would be the way out to make this flow of question->answer possible?
My code so far:
let messaging_events = req.body.entry[0].messaging;
for (let i = 0; i < messaging_events.length; i++) {
let event = messaging_events[i];
if (event.message && event.message.text) {
let text = event.message.text;
let textid = event.message.mid;
}
}

You can use this module facebook-chat-api.
Or read it to learn how to create it.
And are you using facebook-graph-api or login with NodeJs ?

Related

How to clear chat NODE JS telegram bot (node-telegram-bot-api.)

Hi guys i'm junior nodejs developper!
my question: How to clear chat on write text == cls
my code
const { chat, message_id } = message
const chatId = message.chat.id
const name = message.from.first_name
const text = message.text
// ================== on write "cls" clear chat
else if (text == 'cls') {
bot.deleteMessage(chatId, chat.id)
var msg = message;
}
I think currently there isn't any method to clear whole chat using bot but
you can use simple loop to delete last 100 messages(only work for group chats and bot should have admin permission to delete messages). Like this,
const Telegram = require('node-telegram-bot-api')
const TOKEN = process.env.TOKEN || "<YOUR BOT TOKEN>";
const bot = new Telegram(TOKEN,{polling:true})
bot.onText(/\/start/,(msg)=>{
bot.sendMessage(msg.chat.id,'Hello World!')
})
//I use bot command regex to prevent bot from miss understanding any user messages contain 'cls'
bot.onText(/\/cls/,(msg)=>{for (let i = 0; i < 101; i++) {
bot.deleteMessage(msg.chat.id,msg.message_id-i).catch(er=>{return})
//if there isn't any messages to delete bot simply return
}
}
)
Hopefully this might help you :D
Refer:
https://core.telegram.org/bots/api#deletemessage
Note: I'm a beginner, so if there are any mistakes, please excuse me.

How can I tell if a sent message is the author's first?

I'm currently making a bot that welcomes new users by sending a message right after the author's very first message. To clarify:
User joins
User sends first message
Function is called
How can I determine if the author has sent a message in the guild before?My only idea at the moment is just to add a row for each new member (sqlite), and when they sent a message, the row will be deleted and the bot can welcome the user.
Yeah you need a database for that, I don't think discord keeps the count of how many messages you send in each guild, at least I didn't find anything looking in discord.js docs.
Luckily we can fetch all channels, and then fetch messages from the channels:
client.on('message', async message => {
let totalUserMessages = 0;
let txtChannels = Array.from(message.guild.channels.cache.filter(c => c.type === 'text').values());
for (var i = 0; i < txtChannels.length; i++) {
let channel = txtChannels[i];
let messages = await channel.messages.fetch();
let userMessages = messages.filter(m => m.author.id === message.author.id);
let numOfMessages = Array.from((userMessages).values()).length;
console.log(numOfMessages);
totalUserMessages += numOfMessages;
}
console.log(totalUserMessages);
if (totalUserMessages === 1) {
// do stuff here
}
});

Telegram bot - how to deletemessage from string from list of array

how do i do i ask my bot to delete a message send by user based from a list i have.. i tried with the code below but not working :(
Example 1:-
user message : I take orange every morning.
var what = ["apple","orange","grape","kiwi","mango","banana"];
or
var what = "apple,orange,grape,kiwi,mango,banana";
for (i=0; what.lengt;i++){ ` if(msg.text.toSting().toLowerCase().includes(what[i].toString())) {
//bot.kickChatMember(msg.chat.id, msg.from.id);
bot.deleteMessage(msg.chat.id, msg.message_id)
}
}
please guide pray

Welcome message Bot Framework v4 nodejs

I'm developing a multichannel bot (focusing on web and telegram) that's based on Microsoft's Bot Framework (https://learn.microsoft.com/en-us/azure/bot-service/?view=azure-bot-service-4.0)
I'm stuck with the initial message the user is getting.
My bot is based on the complex bot published by Microsoft: https://github.com/Microsoft/BotFramework-Samples/tree/master/SDKV4-Samples/js/complexDialogBot
Issue I'm seeing is that in the emulator bot is working great, on the web user is not greeted with the welcome message. I've used iframe to integrate the bot.
I'm checking activity types and when members are added to the chat, but seems like it's not triggering on the web.
if (turnContext.activity.type === ActivityTypes.ConversationUpdate) {
if (turnContext.activity.membersAdded && turnContext.activity.membersAdded.length > 0) {
await this.sendWelcomeMessage(turnContext);
}
}
I saw similar questions asked but either for bot framework v3 or C# implementation (like this one Welcome message not visibile in Webchat,but work in Emulator and Welcome Message not working in Azure portal using Microsoft Bot Builder SDK(v4) for nodejs)
try using this
enter code here
this.onMembersAdded(async context => {
const membersAdded = context.activity.membersAdded;
for (let cnt = 0; cnt < membersAdded.length; cnt++) {
if (membersAdded[cnt].id == context.activity.recipient.id) {
const welcomeCard = CardFactory.adaptiveCard(WelcomeCard);
}
}
});
You can solve your problem in below code
in iframe to integrate the bot you can write member Adding code copy in inside !turnContext.responded
if (turnContext.activity.type === ActivityTypes.Message) {
if (!turnContext.responded) {
await this.sendWelcomeMessage(turnContext);
}
}

dialogflow Webhookclient "request_" property

I am trying to build up a facebook messenger chatbot using Dialogflow. In the dialogflow fulfillment inline editor, I found that I can use agent.request_.body to get the body of the request. I assume "request_" is a property of WebhoodClient object? But I couldn't find any documentation elaborate that, could you please advise if my understanding is correct and where I can find the reference or documentation?
const agent = new WebhookClient({ request, response });
console.log(JSON.stringify(agent.request_.body));
Thanks
Google provides documentation for Dialogflow webhooks here, which include this sample webhook to inspect parameters and dynamically create slot filling prompts:
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
function flight(agent) {
const city = agent.parameters['geo-city'];
const time = agent.parameters['time'];
const gotCity = city.length > 0;
const gotTime = time.length > 0;
if(gotCity && gotTime) {
agent.add(`Nice, you want to fly to ${city} at ${time}.`);
} else if (gotCity && !gotTime) {
agent.add('Let me know which time you want to fly');
} else if (gotTime && !gotCity) {
agent.add('Let me know which city you want to fly to');
} else {
agent.add('Let me know which city and time you want to fly');
}
}
let intentMap = new Map();
intentMap.set('flight', flight);
agent.handleRequest(intentMap);
});
My guess would be to add
console.log(agent);
right before defining the flight function, then checking the logs to see which objects agent contains, then adding iterations of console.log(agent.fakeObjectName) until you find the information you're looking for.
If you're following the deployment process recommended in Actions on Google's Codelabs level 2, your logs will show up in the Firebase console, like this:
Hope that helps!
Just a note.
I had a code similar to this:
const city = agent.parameters['geo-city'];
There is an icon that suggest it's better written in dot notation.
that is gone after I changed it to:
const city = agent.parameters.geo-city;

Resources