Multiplayer call to Telegram bot - python

I'm writing a telegram-bot. It checks the email box(common to all), and if it finds the set parameters then returns some information to the user and delete this email that contains this parameters. So the problem is this: when I call this bot alone, everything's allright, but if I cal it from several accounts at the same time, it doesn't work correctly. It either doesn't send the information, or sends it incorrectly. It sends the info which belongs to other user. I can't solve this? Can anybody advise me somethin? Sorry for my Ruslish)))

Related

How can I monitor (test) and view the DMs my Slack Bot is sending?

I've created a slack bot that sends out a private message via the client.chat_postMessage method to different users in my workspace.
They've received the message (they showed me screenshots), however, I cannot see their private conversation via Slack it seems. Is there another way to monitor that the message has arrived and looks the way it's supposed to be (the format, attachments, etc. sent together)?
I tried this method:
response = client.conversations_info(
channel=,
include_num_members=1
)
but when I input the user's channel it returns that the channel doesn't exist.
Here's what my bot's message looks like in Slack according to the receiver. I cannot view it anywhere it seems.
If you're using your bot token to send a message to a user, the message will show up in a DM between your bot and that particular user. Your bot is its own user, separate from your user. For that reason, if you login to Slack, you won't be able to view that conversation as you yourself are not a part of the conversation.
The only way to "view" a message that has been sent to a user within a DM is to call the conversations.history method. Use the channel ID of the DM between the bot and your user, which will start with "D".
If you want to confirm how a message will look like within Slack, send the message to yourself beforehand. Additionally, adding logging within your code will help for any troubleshooting that you may need in the future.

How to update a message text on telegram api ? (Python)

I'm developing a telegram bot using telepot, I'm trying to ask the user a question for example: What's your name?
Then wait for the user to type the name, and save it in a variable, but this is not a library method, I think.
So I decided to create my own method.
When asking the question "What is your name?" I will go to the telegram api and I will change the text field to empty so in the next function I will use a loop that does not break until the user's last message is different from empty, understand?
I access: https://api.telegram.org/bot/(enter image description heretoken)getUpdates
and I want to change this session:
(Change a message send by user)enter image description here
"message":{"message_id":1133,"from":{"id":5296812825,"is_bot":false,"first_name":"Vinicius","language_code":"pt-br"},"chat" :{"id":5296812825,"first_name":"Vinicius","type":"private"},"date":1652800679,"text":"Mensagem from User"}}] }
for
"message":{"message_id":1133,"from":{"id":5296812825,"is_bot":false,"first_name":"Vinicius","language_code":"pt-br"},"chat" :{"id":5296812825,"first_name":"Vinicius","type":"private"},"date":1652800679,"text":"EMPTY"}}] }
No; Bot's can't alter messages send by an user
A Telegram bot can only alter/delete his own messages.
Luckily, otherwise every message you send could be altered by some random bot, this would lead to insecure behaviour for sure.

Open another Telegram chat/group/channel using Bot

I suspect it may be rather kid question – but anyway.
How to open another Telegram chat or group or channel using pyTelegramBotAPI? I want to forward the user (not message, the user himself) to another channel if he clicks certain button.
I saw content type migrate_to_chat_id in Message class declaration. Should I use it? If so, how to get an id of channel I need? It won't send any message to my bot.
I would better use "t.me/..." url.
Partly solved.
Speaking about the buttons, it is indeed easy. You just use named parameter url= in InlineKeyboardButton() method.
For other cases. You need to open another channel(s) from function depending on several conditions for instance. Still don't know. Import requests and make GET request? I suspect that something for it should already be in pyTelegramBotAPI, but searching in lib files wasn't successful.

How do I save/load Bot Framework session information to/from the local disk?

I have a question about a Microsoft Teams Python bot. If the bot has been added to some personal chats and group chats and I restart the bot, sometimes the bot needs to be added to the chats again. So I want to make bot sessions.
Is it possible to make a bot session in Microsoft Teams? I want to store session information on the local disk, and then have the bot load that data when it starts.
My bot code is very similar to this sample.
Thank you for your help.
Updated:
Like I said my bot code is very similar to this sample, but a bit different. Because of this I created an example for this question. First of all I create a bot in Azure and set up it.
After this, in my bot's config.py file I set up the port and Microsoft app ID and password (generated by clicking the "Manage" button).
import os
""" Bot Configuration """
class DefaultConfig:
""" Bot Configuration """
PORT = 3978
APP_ID = os.environ.get("MicrosoftAppId", "sadsadsadasd")
APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "asdasdasdasdasd")
After this I execute the command ngrok http 3978 and put the generated endpoint in the Azure bot configuration. To register the bot as an application, I use App Studio in Teams. After I do that, I just to need run the bot in CMD so I run a command like python run.py
After I run the bot, I can add it in the channel and run commands and functions that I created in the code.
This is just an example of how I set up the bot. The main bot is on a Linux server.
Here is why I want to make the bot keep session information and load it after the server or bot was restarted. Sometimes after I restart the bot or server it is no longer in the chat or team. In the future I want make some kind of commands and execute them using a cron job or something like that.
If the bot disappears from a chat then I can't use bot commands in that chat. For example, I add two bots in a chat. After I restart one of them I can't get any response from it like in the picture below.
And with # I can't see the bot.
I have an idea. After I add the bot in the chat I get this in the console:
Adding new conversation to the list: {'additional_properties': {}, 'activity_id': '123215513', 'user': <botbuilder.schema._models_py3.ChannelAccount object at 0x0000027C0ED60>, 'bot': <botbuilder.schema._models_py3.ChannelAccount object at 0x0000027Cs2FD0>, 'conversation': <botbuilder.schema._models_py3.ConversationAccount object at 0x0000027C0400>, 'channel_id': 'msteams', 'locale': 'en-US', 'service_url': 'https://smba.trafficmanager.net/emea/'}
Formatted:
{
"additional_properties": {},
"activity_id": "123215513",
"user": <botbuilder.schema._models_py3.ChannelAccount object at 0x0000027C0ED60>,
"bot": <botbuilder.schema._models_py3.ChannelAccount object at 0x0000027Cs2FD0>,
"conversation": <botbuilder.schema._models_py3.ConversationAccount object at 0x0000027C0400>,
"channel_id": "msteams",
"locale": "en-US",
"service_url": "https://smba.trafficmanager.net/emea/"
}
So if I store this information and then load it when I start the bot, maybe it will work?
There is an option inside a bot to save a transcript of the conversation, but that's kind of unrelated. Basically, the important thing to know is that you don't need to store anything your side - from the user perspective, the entire conversation history is preserved in the Teams client, and from the perspective of your bot, storing the entire conversation history doesn't really gain you anything - user state is more relevant than conversation history. This would be storing an object in a persistence you choose (e.g. database, nosql store, azure blobs, whatever), but it would be state you choose to store for the user (basically whatever properties make sense to store for your app, in a kind of "User" collection). This is definitely a possible and often necessary concept, and this link will be useful for you: https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-concept-state?view=azure-bot-service-4.0
It's important to know though that this is a separate idea from sending a message on a defined schedule (e.g. Cron) to a user. For this to work, you need to read up on a concept called "Proactive Messaging". I have a sample on the Teams PnP gallery specifically dealing with it (code only in Node and Dotnet - no python I'm afraid, but hopefully it's useful for you). See here for that. Note that at the bottom of this link is a list of further reading on the topic as well.
Where the two above ideas come together is that you need to store certain state about the user to be able to send the proactive message later. In the sample I link to, I show how to get the settings you need to send the proactive message, but I've not included the concept of saving them to a data store - that's up to your own implementation inside your bot (e.g. SQL Azure, MongoDb, blob, whatever).
Also important to note (and I think part of the confusion and in fact part of why I wrote the sample) - your proactive code does not need to live in the same set of code as your bot! Your bot could be a web service running somewhere, and your proactive code an Azure Function/Lambda/similar.
Like Hilton said, it's a bad idea to try and save bot state to the local disk. Also, there's no need for that. Both Hilton and I have linked to documentation that should help you understand how bot state is meant to be saved.
I cannot reproduce the problem you're encountering where the bot gets removed from Teams chats. The problem sounds impossible anyway, based on how Teams works and how bots work. Teams should have no way of knowing whether your bot is started or stopped. It's possible that your server is set up to manually uninstall the bot from Teams conversations based on when the bot starts and stops, but that would still be very strange. I'm willing to continue troubleshooting this with you, but I thought I'd post an answer now in case you'd like to award your bounty to someone before it expires.

Sending a message with PRAW but the receiving account does not ever get the message

I'm trying for the first time using APIs with Python. I'm using PRAW. I've wrote a stupid easy script trying to send a message from one account to another. I'm prompted to login and enter the captcha, but then the receiving account does not ever get the message. What am I doing wrong? I need to get the foundations down to be able to progress onto a more complicated program that implements some logic and other methods.
import praw
r = praw.Reddit('PRAW related-question monitor by u/testpurposes v 1.0.')
r.login()
user = 'krumpqueen'
msg = 'Hello'
r.user.send_message(user, msg)
Your code appears to be correct. The r.user.send_message() takes two arguments. The first is the user to receive the messages and the second is the actual message. So, your code is accurate. However, you shouldn't be receiving a captcha all the time. Try entering your reddit login info in the r.login() method. It should look something like r.login('username', 'password'). You might also see how much karma the account has. Your script may not be making it all the way through because of low karma.
You're sending the message to yourself with subject 'krumpqueen'.
Try:
r.send_message(user, 'SOME SUBJECT', msg)

Categories

Resources