Telegram: Cannot send message/photo into a channel with Telegraf (NodeJs) - node.js

I would send a message on telegram channel with telegraf. I'v einvited the bot and put him admin.
I've tested with this code:
bot.on('text', (ctx) => {
// Explicit usage
ctx.telegram.sendMessage(ctx.message.chat.id, `Hello ${ctx.state.role}`)
// Using context shortcut
// ctx.reply(`Hello ${ctx.state.role}`)
})
bot.launch();
But it replies only if i wrote on private.
So why it doesn't work on a channel?
Than how can i send a message in that channel without a command? (For example with and interval?
I i try this one:
bot.use((ctx) => {
console.log(ctx.message)
})
when i use the bot on private chat (with him) it returns all the message data. On the channel i receive undefined

In your case CTX have current chat info, if you want to send message to channel provide correct id as documented for Telegraf sendMessage:
telegram.sendMessage(process.env.TELEGRAM_CHANNEL, ctx.message.text);
I am using a bot for a public channel, so in my case it's:
TELEGRAM_CHANNEL=#MY_PUBLIC_CHANNEL_NAME
The channel name is available in channel info settings t.me/MY_PUBLIC_CHANNEL_NAME

Related

How to send DM messages after triggering comand Dsharp+

I'm making a Discord bot and i'm in trouble. Idk how to send direct message after triggering command by user, i tried to use RequireDirectMessage, but after this command just became inactive, i couldn't trigger it neither in discord Guild nor in direct message.
You need to create a DM channel and then send a message to that channel like this
[Command("senddm"]
public async Task SendDMExample(CommandContext ctx)
{
var DMChannel = await ctx.Member.CreateDmChannelAsync();
await DMChannel.SendMessageAsync("WhateverYouAreSending");
}

BotFramework-DirectLine JS - Initial Message Missing

I have a Bot I have built with MS BotFramework, hosted on Azure. The Bot is built to start the convo with a Welcome Message. When I test the bot through emulator, or on Azure test webchat, the bot will initiate the conversation as expected with the welcome message.
However, in my chat client using BotFramework-DirectLineJS, it isn't until I send a message that the bot will respond with the Welcome message (along with a response to the message the user just sent).
My expectation is that when I create a new instance of DirectLine and subscribe to its activities, this Welcome message would come through. However, that doesn't seem to be happening.
Am I missing something to get this functionality working?
Given this is working for you on "Test in Webchat", I'm assuming your if condition isn't the issue, but check if it is if (member.id === context.activity.recipient.id) { (instead of !==). The default on the template is !== but that doesn't work for me outside of emulator. With === it works both in emulator and other deployed channels.
However, depending on your use cases you may want to have a completely different welcome message for Directline sessions. This is what I do. In my onMembersAdded handler I actually get channelId from the activity via const { channelId, membersAdded } = context.activity;. Then I check that channelId != 'directline' before proceeding.
Instead, I use the onEvent handler to look for and respond to the 'webchat/join' event from Directline. That leaves for no ambiguity in the welcome response. For a very simple example, it would look something like this:
this.onEvent(async (context, next) => {
if (context.activity.name && context.activity.name === 'webchat/join') {
await context.sendActivity('Welcome to the Directline channel bot!');
}
await this.userState.saveChanges(context);
await this.conversationState.saveChanges(context);
})
You'll still want to have something in your onMembersAdded for non-directline channel welcome messages if you use this approach.

How To Send A Discord.JS Startup Message To All Servers

I wanted to inquire on something I’ve been trying to do recently. I have a Discord.JS bot, and it’s pretty public (130+ servers), and I want it to send a message to any channel in every single server it’s in, when the bot starts up, the purpose is to create a “New Update” notification in one of the channels.
Please do not plan on doing this, as this will get you rate limited really quickly.
This is kind of possible. There is a problem, there is no default guild channel, so you cannot get their "main" channel just by their guild object. If you would want to do that you, would need them to select their channel and store that ID somewhere.
This is a simple solution, where you loop through every guild of your Discord client, and send some channel a message. You don't know what channel with this solution specifically, but you will send some channel a message if it is not a voice channel.
const Discord = require("discord.js");
require("dotenv").config();
// Creating our Client
const client = new Discord.Client();
// Event listener: when the bot is ready
client.on("ready", () => {
// Looping through every guild the bot is in
client.guilds.cache.forEach((guild) => {
// Getting one of their channels
let channel = guild.channels.cache.array()[2];
// Sending the channel a message
channel.send("Hey");
});
});
client.login(process.env.DISCORD_BOT_TOKEN);

How to get all non bot users in discord js using a discord bot in nodejs

I have created a discord bot by taking reference from this digital ocean link.
Now I can send message to any channel using the bot but my requirement is to send dm to user of that server.
For that I have tried many SO answers and followed other links, but all the solutions end up to be same.
I have tried this two way to get the users of a guild and send dm to any one selected user.
1st way - Get all users of guild (server)
const client_notification = new Discord.Client();
client_notification.on('ready', () => {
console.log("Notification manager ready");
let guild = client_notification.guilds.cache.get("Server ID");
guild.members.cache.forEach(member => console.log("===>>>", member.user.username));
});
client_notification.login("login");
Output
Notification manager ready
===>>> discord notification
By this way it only returns me the bot name itself. Although the membersCount is 6.
2nd way - send dm to user directly (server)
client.users.cache.get('<id>').send('<message>');
It gives me undefined in output.
My configs,
Node version: 10.16.3
discord.js version: 12.5.1
My question is how to get all the guild members in discord.js?
Discord added privileged gateway intents recently. So to fetch all member data you need to enable that in the developer portal. After that you need to fetch all the members available, for that we can use the fetchAllMembers client option, and then we need to filter out bot users, so that we don't message them.
const client_notification = new Discord.Client({fetchAllMembers:true}); //Fetches all members available on startup.
client_notification.on('ready', () => {
console.log("Notification manager ready");
let guild = client_notification.guilds.cache.get("Server ID");
guild.members.cache.filter(member => !member.user.bot).forEach(member => console.log("===>>>", member.user.username));
});
client_notification.login("login");
The .filter() method filters out all the bots and gives only the real users.
You should avoid using fetchAllMembers when your bot gets larger though, as it could slow down your bot and use lots of memory.
I think the problem is related to updating the bot policy for discord. Do you have this checkbox checked in the bot settings?
https://discord.com/developers/applications
Some info about client.users.cache:
Its a cache collection, so if you restart bot, or bot never handle user message or actions before, this collection will be empty. Better use guild.members.cache.get('')

How to retrieve the offline messages from pubnub?

I am trying to create a chat application using pubnub api in javascript for first time
Below described is the logic i created for implementing a chat
User A is subscribed to channel "talktoA" and "ourPublicChannel"
User B is subscribed to channel "talktoB" and "ourPublicChannel"
When a User A want to talk to User B User A will a message to channel "talktoB"
as user B is subscribed to channel "talktoB" User B will receive the message and vice versa
When users want to send broadcast message the users need to send message to channel "ourPublicChannel"
Following are the code for each operations
1. **Establish a Connection**
var pubnub = PUBNUB.init({
publish_key: 'pub-mypublishkey',
subscribe_key: 'sub-mysubkey',
uuid : me
});
2. **Publish Message to a Channel**
//Sending a private message
pubnub.publish({
channel: ['privatechannelofB'],
message: {
text: “Test Message to userB ”,
username: me
}
});
//Sending a broadcast message
pubnub.publish({
channel: ['publicchannel'],
message: {
text: “A Broadcast Message to all user”,
username: me
}
});
3. **Subscribe /Receive to a channel**
pubnub.subscribe({
channel: ['myprivatechannel','mypublichannel']
message: function(data) {
alert(data)//Test Message
}
});
4. **History of message**
pubnub.history({
channel: channelname,
callback: function(m){console.log(m)},
});
I need to confirm the following
How to retrieve the offline messages ? if user A send message to user B and user B is offline i need to show the offline
messages?
History api will give the full list of message but how sort it whether it is offline messages
Is the approach right?
The Playback and Storage (history API) lets you to retrieve the message history of a channel up to 30 days retention.
When userA sends a message to userB, who is not connected to the internet, or the application is in the background, no problems, userB will be able to retrieve every message that was sent to his channel in the past 30 days.
Otherwise there is no difference between "offline" and "online" messages. If a message was successfully sent, you can retrieve it with the history API.
You can also use the Mobile Push Gateway for push notifications, in this case your user will receive the message when the app is in the background state.
For the best user experience I'm combining the two stuff and had zero problems with receiving messages.

Resources