I have a script for a discord bot that I need to use in a Django App so I can reference the DB and Models from it. The goal is to dynamically output messages to discord based on records.
In Rails with the discord gem: https://github.com/discordrb/discordrb
I am able to add my script to the initializers folder. This way it starts up and it's always listening. When something like a message or command comes through, I can dynamically update the output message based on the information from the command or message from Discord.
Here is my app\config\initializers\discord_bot.rb file:
require 'discordrb'
bot = Discordrb::Bot.new token: '<>'
bot.message(with_text: 'Ping!') do |event|
task = Task.find(1)
event.respond "Pong! #{task.title}"
end
bot.run
I can access the models from here. This is a quick example but if I wanted, I can check against the discord information I receive to dynamically respond with the bot information from the database records.
How am I able to do something similar to this in Django?
Related
I am building a telegram bot that will automatically add messages to preexisting menus in a bot ( created by Manybot, not with python ) once they are uploaded to a specific channel.
I spent a week or so trying to find a way to update preexisting menus but it can't manage to find a solution.
even when I try to run an infinite loop to monitor any messages to the bot ( using any telegram-python library out there ) i get this error :
Conflict: can't use getUpdates method while webhook is active; use deleteWebhook to delete the webhook first
and i am forced to delete the webhook , which will in turn disable every command i added to the menus and need to create another bot
I would really appreciate any help .
The problem is not related to existing webhooks, it's the fact that each bot token can poll / webhook only once.
In other words, you can't have two scripts running with the same bot token, regardless if it's long polling or a webhook.
This is a limitation of the Telegram Bot API.
You can only run your own script when the manybot version of your bot is paused, stopped, turned off, etc. The getUpdates method will reject new connections with the same bot token.
You can either use a second bot token to use other commands if your setup allows it, or use the MTProto API with frameworks such as Pyrogram or Telethon.
My goal is to make a Telegram Bot using Python that does the following:
The user types a command
The bot explains what the user should type next.
The user types certain information
I use that info to fetch a value in a python dictionary.
The bot replies with that info
I have already created the bot and set the command. The problem is that I don't know how to keep the bot "listening" . I have looked up in the docs but I have only found getUpdates, which gets the user's responses but only when you hit run.
res=requests.get(url=f"https://api.telegram.org/bot{bot_token}/getUpdates")
I would like to set a webhook, and I know there is a method for this but I'm not shure about how this works.
When you are a PHP programmer, setting the webhook through a URL and using setWebhook would do the trick and telegram will send the result to this link whenever a user sends an update to the bot. But, Python programmers have to use a different approach, I think. The main, and while, the easiest approach to make the bot listen permanently to the request is to python-telegram-bot module.
There are a few links that can help to build your first pythonic bot that can respond to users' updates.:
Python-Telegram-Bot Repository, which is the repository of the module.
A few examples of bots created using this module, can give you insight into the process of creating your first bot.
A conversation bot
I have made a discord bot in python which makes use of databases to store channel ids where my bot is running. I have a command which allows me to add/remove items from this database, however, it will not actually update the database unless if I reboot the bot. For reference, this bot is written in discord.py and is written on repl.it. This is all I have code-wise, so far:
db['channel_ids'] = channel_ids #just to show which database I want to reference
#client.command(name = 'update_database')
async def update_database():
# want this function to simply make the database update/reload, if this is at all possible.
To add more information, I have a command which allows me to see all of the channel id's which my bot is running in. However, as mentioned, the list it returns may be outdated if I have added/removed some channel ids and not yet rebooted the bot. All I want is a way to get the database to update without having to reboot the bot. Thanks all.
I have 2 discord client instances a bot and a user account. I have already figured out how to copy messages from one channel to another channel, basically a on_message event. However, I want to know if there is a way I can use the user account to fetch the messages, I've tried fetching messages in different guilds but they didn't work because the bot was not in the server and the bot is fetching messages. I've tried using the user account instance to do it but it didn't really work.
The comments were there as a guide for myself because I'm not that familiar with Python it's fairly new to me. If anyone knows how I can use the user account to get or fetch the messages I'd appreciate the help...
PS: I know that some of the code isn't needed, I haven't removed anything that isn't needed. I wanted to actually finish the project before inspecting it.
I just wanted to know that I have tried this myself and it is not possible. Because from discord.py's Docs. Which you can find down below, does not allow users (user account) to transfer data to a bot since you would need to request from discord API which leads to user getting banned (Very quickly) theere are some work arrounds but I would suggest not doing so since self botting is against the TOS of discord
https://discordpy.readthedocs.io/en/stable/
EDIT: Another thing is that discord API blocks users from fetching or getting anything using a user account with discord.py
Telegram allows the commands to be updated using setMyCommands. I can successfully update the commands in realtime based on the user input using a python API, pyTelegramBotAPI.
However, the problem is the user has to exit the chat with the bot and then come back to the chat again to see the new commands (by typing /).
Is there any way that I can have the bot update the list of commands in realtime with the user still in the chat?
Actually it is possible to do this real time if you set the scope to chat with specific user. There is parameter scope in set_my_commands function. By default set_my_commands affects all the chats of your bot. However, you can provide telegram.BotCommandScopeChat as a value of scope param. See the code below, it updates the command menu immediately without need to switch chats.
def _set_menu(commands: List[BotCommand], update: Update):
''' sets the chat commands menu '''
bot = Bot(os.getenv('TELEGRAM_TOKEN'))
bot.delete_my_commands()
if update:
bot.set_my_commands(
commands=commands,
scope=telegram.BotCommandScopeChat(
chat_id=update.effective_chat.id
))
else:
bot.set_my_commands(commands=commands)
So I tried it with telegram client (on linux and on android) and the commands do not change unless the user re-enters the chat. This I think is because the telegram-client only loads the commands when the user enters the chat.
But I also tried this with telegram web and found that the commands changed immidiately after I changed the command sets from BotFather. The webapp actually did load the command set without me leaving the chat, after sending a single message/command
So its definitely a problem with the telegram-client.