Telegram Bot - Does chat id remain same for all the time? - telegram-bot

I am making a telegram bot, which requires a registration and further usage of bot's services, regularly updating user's data.
I have some cases, when I need to edit user's data, but all I can receive from chat is chat.id.
I am planning to use it as a unique value for identifying specifically that user.
The question is, does this attribute remain same for all the time user interacts with bot? I suppose that it will change, when user deletes chat, but will it change in other cases?

The chat.id of a Chat between a bot and a User will never change. In this situation the chat.id represents the id of the User.

Related

Telegram Bots: Can I now start a chat or not?

https://core.telegram.org/bots clearly says:
Bots can't initiate conversations with users. A user must either add them to a group or send them a message first.
But if I visit https://telegram.org/blog/login it says
Telegram bots are a powerful communication tool, but until today they couldn't start a conversation. Even if you wanted them to reach out to you, you had to chat them up first.
With the new login widget, moving from interacting with a website to a conversation on Telegram becomes completely frictionless.
So it is somehow possible to start a conversation via the login widget. But how do I do it?
I get a user id. Is there an api endpoint to initiate a chat with the user?
The only way to begin conversing with a Telegram bot is to start the conversation with them yourself. There's no way for the bot to start a conversation with a user themselves.
As you said, telegram clearly says there is no way for bot to start a conversation, the only thing bots can do is sending messages to an old conversation (I mean a user have to start the conversation).

How to manage in RASA an unique user_id for many channels in parallel?

I have to design an e-learning tutorbot that has multiple possible channels living in parallel:
A live chat (to support learner with FAQ when browsing the e-learning application website)
A IM chat as Telegram (for asyncronous notifications)
An email (for anything else)
...
Take a part the connectors gateway communication logics.
My problem/question is mainly about how to manage user identification.
I want that a user is identified with an unique identifier (call it conversation_id) regardless of channel.
In the mentioned scenario user has one different ID for each channel:
An account_id on the website (e.g. solyarisoftware)
A chat_id on Telegram (e.g. 1234567890)
An email_address (e.g. giorgio.robino#blabla.com)
So, how to let RASA chatbot accept requests coming from the same user (but from different channels)? In other words, how can I map multiple channel IDs into a RASA unique ID, say the conversation_id ?
At the end of the day I need to generate a lookup table that map a UUID with all channel ids. Right?
But is not clear to me what's a good (simple) user experience. At first glance seems to me that that I need a sort of login/registration flow: each time user submit a request on channel X, only for the first time, the user must identify himself somewhere (with his email/account_id?). So by example trough Telegram, when the Telegram connector server receive a /start command, the bot must ask the user email(or account_id), to be able to associate 1-to-1 the TG chat_id with that email. Not perfect in terms of security, I admit.
Any experience/idea/suggestion?
Thanks/giorgio
I don't have an hands-on experience on this but it is something I have been thinking myself for a while.
Indeed the 'merge' of the different accounts (across channels) is something that we need to maintain ourselves, making sure each channel user ID is eventually mapped to a universal user ID (lookup table makes sense to me).
Since you have a website, an email address and multiple channels I would say the assumption is that users first registered on the web site. Each user has a unique email address and obtains an account secured by username/password, as well as a unique secret token.
When chatting on browser (ie Rasa WebChat) you can initialise the plugin with custom parameters (ie userId=1 or token=X), in this case the chat session is assigned to the given user id.
When using Telegram (or other messenger application) I would (at the very first access) ask the user if he/she has already registered. If yes then I would ask the token: this can be used to perform the map and link this Telegram user to the universal user Id. Same approach for other channels.
Depending on your needs you might need to deal with anonymous access (users don't provide the token) or maybe you want to provide a different token for each channel (more secure, but requires more work and an easy way to explain this to users).
Finally something to look at (I haven't tried myself) is the possibility to provide in the web site the option to open Telegram (or others) passing an extra parameter, which would allow you to perform the mapping. I don't know if this is possible, but it would simplify this process for your end users. Or the other way around: the users in Telegram could be provided with a link which opens the website, where they can login and be recognised.

Node-Red Telegram chat through two bots

I want to design a chat system through Node-Red that two Telegram bots which in one side there is an operator who has the other side's Telegram user ID. I designed it so it asks for user ID, then asks for message, and sends it to the other bot, and the other person only can write to the operator, but it does not work.
Well, I do not know how to transfer one variable from one node to the other without making it global.
Can you help me?

Identify unique user in Dialogflow V1

So I am testing out Dialogflow and one of the first questions I have is: how does my bot know who it is talking to? I need to identify a user and keep that information for as long as I can. The basic scenario being:
User starts his/her first conversation.
Chatbot send a fulfillment request to the server trying to match a user within its own database.
The user is found, the information (as a JWT or some other token) is sent back to Dialogflow and stored there for further communication. In reality, this part would involve asking for user email, sending a verification code to that email and then verifying the user with the code.
User then starts chatting with a bot and all fulfillment requests get the unique token stored for this very user, so that my REST API knows which user is being served with the response.
Couldn't find anything about it in the docs (maybe I am looking in the wrong places).
There will be several integrations, like Messenger, Viber, Telegram. I dunno, maybe those APIs add some unique information on the user?...
Thanks for the help!
Sorry, I know it's been a while, but maybe this will help someone else.
The right solution here is a user id, not a session id. A user id is provided by the chat platform (Facebook, Slack etc) and is consistent across sessions for the same user.
To get the user id, go to the Fulfillment tab, enable the editor and use a function like so:
let r = request.body.originalDetectIntentRequest
//this makes sure that you're on an integration
if (r["source"]){
return r.payload.data.sender.id;
}
To tie together ids from different platforms, you probably have to have some kind of log-in process every time you encounter a new id on a platform.
Pop,
Sessions are built in already into DialogFlow requests to your fulfilment service, if you check the payload you will find a sessionId, it remains the same for the same client until it expires.
However if you want to identify the user from any of the clients that you can connect to DialogFlow like Messenger then from the same request payload to you you will notice that there is an object named originalRequest that is only available when requests are coming from those clients.
You can personalize those users response eg using their FB firstname in a message to them.

Telegram bot questions

User1 has a Telegram account and created the Telegram bot Bot1.
User2 has a Telegram account.
How User 2 can work with Bot1? i.e. in teh same way as User1 does it: send commands etc.
How Bot1 can send messages to User2? I.e. report about smth etc
You mean work with BotFather? No.
Each user can access to its own bots using BotFather and you can't access other bots from another accounts. But if you mean work with tokens, then yes you can work with bots that you have their tokens.
Getting last updates from telegram: https://core.telegram.org/bots/api#getupdates
NodeJS library: https://github.com/yagop/node-telegram-bot-api
Okay let's break your questions into parts:
How User 2 can work with Bot1?
When you say work with its kind of relative. There are two assumptions I can
come up with:
(I). It means that User2 wants to send commands from his
telegram app on his mobile device to bot1(bot1 was made by User1), in this
case anyone can send messages (any form of text) and commands (like
/showusers) to any bot even though they were not its creator. Also just a
side note this is from the telegram bot docs
Bots can't initiate conversations with users. A user must either add them to a group or send them a message first. People can use telegram.me/ links or username search to find your bot.
(II). On the other hand work with can mean that I can program how bot1
interacts with its users, e.g if a user sends /ping to the bot reply with a
message pong. In this case only User1 can work with bot1 because he has the
bot token. However if User1 gives his Bot token(its like your credit card
details) to User2 then User2 can program that bot to do anything he wants.
So to answer your question, if you mean my first assumption then User2 only needs telegram installed and the name of Bot1 but if you mean assumption 2 then User2 needs the bot token from User1 to work with it.
How Bot1 can send messages to User2? Every user has a unique chatid that telegram sends to you the first time someone clicks the start button of your bot(actually they send the chatid every time a person uses your bot). All you need to do is to store this chatid in a database or something and then when you want to send the person a message you indicate the chat id. Read about the sendMessage method made available by the API to better understand how to use it.