What is the keep_alive library and will it work? - discord.py

I saw a question for a discord bot and it was using a library called "keep_alive". How can I import it to my bot if it does keep the bot online?

The below code is the keep_alive code which prevents your repl from dying by creating a flask server. You should use uptime-robot to make the server gets pinged in a certain amount of time to make it work for long time without stopping.
from flask import Flask
from threading import Thread
app = Flask('')
#app.route('/')
def main():
return "Your bot is alive!"
def run():
app.run(host="0.0.0.0", port=8080)
def keep_alive():
server = Thread(target=run)
server.start()
You should create a keep_alive and paste the above code there and then add keep_alive.keep_alive() in your main file to make it work.
You can refer this youtube-video to understand how to use that

Related

Using client separately in python pyrogram

I have got this problem with pyrogram
app = Client("session",
api_id,
api_hash,
bot_token)
How can i send message via bot not userbot ?
for using a bot and a userbot separately in same time you should do this :
#bot.on_message ...
your code
#app.on_message
your code
bot.start()
app.run()
or you can use 2 threads with using threading mudule.

Telegram bot is not sending messages

I'm trying to execute this simple telegram bot but I get no output and I don't know what I'm doing wrong. This is the code:
from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext
def hello(update: Update, context: CallbackContext) -> None:
update.message.reply_text(f'Hello {update.effective_user.first_name}')
updater = Updater('my_token')
updater.dispatcher.add_handler(hello)
updater.start_polling()
updater.idle()
I think you missed some parameters in the add_handler function, so it should be like the following:
updater.dispatcher.add_handler(CommandHandler('hello', hello))

Command not found error after changing virtual machine

Okay so I have a music bot code that was working before I switched to another virtual machine provider. All the requirements are exactly the same as in my previous virtual machine because I copy and pasted everything including the requirements.txt. The bot runs normally with 0 errors until I try to run any of the commands. It gave me this error:
discord.ext.commands.errors.CommandNotFound: Command "play" is not found
I've tried rolling back to the rewrite version I started the project on,
changed #client.command to #bot.command after assigning bot = commands.Bot(command_prefix='prefix')
#I've assigned client = discord.ext.commands
#client.command(name='play', aliases=['sing'])
async def play(self, ctx, *, search: str):
#then some code
update 1: Ran it as a cog and raised:
discord.ext.commands.errors.ExtensionFailed: Extension 'music' raised an error: TypeError: cogs must derive from Cog
update 2: No idea why rolling back the rewrite version didn't work though. Perhaps I didn't do it correctly.
Just simply run it as a cog.
Note that the way cogs work has been updated recently:
https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html
If you still want to run it as a standalone bot,
your bot should looks like something along the lines of:
from discord.ext.commands import Bot
bot = Bot("!")
#bot.command(name='play', aliases=['sing'])
async def play(ctx, *, search: str): # Note no self
#then some code
bot.run("token")
It's important that the bot that you run is the same bot that you register the commands with. You're also passing self to your bot even though it's not in a cog, which doesn't make sense.
Okay so I found the problem.
The bot doesnt work when I try to run it as a standalone bot.
The reason using it as a cog don't work the first time was because.
the way cogs work has been changed in discord.py rewrite.
These are the changes I made:
#in cogs/music.py
class Music:
#Code
#bot.event
async def on_ready():
print('Music bot online')
to
#in cogs/music.py
class Music(commands.Cog):
#Code
#commands.Cog.listener()
async def on_ready():
print('Music bot online')
Thank you to the legendary #PatrickHaugh for helping me figure this out.

PyQt4 get info including info that is generated by ajax

I want to Use PyQt4 to get all the infomation including these generated by AJAX requests. I tried a few examples, but it didn't achieve my goal.
For example:
amazon
In the page, the item 126 new is generated by AJAX request. I want to get the whole page including the item generated by AJAX request through one request. All of the AJAX requets are carried out automatically.
There's a simple code sample, it couldn't get the items generated by AJAX:
#!/usr/bin/env python
#coding: utf-8
import sys
from PyQt4.QtCore import QUrl, SIGNAL
from PyQt4.QtGui import QApplication
from PyQt4.QtWebKit import QWebView
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl('http://www.amazon.com/gp/product/B0042FV2SI/ref=s9_pop_gw_g107_ir01?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-2&pf_rd_r=1MKC5JJV07SG4WG5PJXY&pf_rd_t=101&pf_rd_p=1263340922&pf_rd_i=507846'))
web.show()
sys.exit(app.exec_())

Programmatically changing wireless router settings - Netgear ideally

Is it possible to programmatically change settings on a Netgear wireless router using C#? I have settings that I change often and I would like to create my own interface for making those changes. Currently I navigate to the admin web page (10.0.0.1) and it prompts me for a username and password. After I authenticate I can use the web interface to change the router's configuration.
If this isn't possible with Netgear, do any outher wireless routers have an API for developers?
There aren't any APIs out there to do this, but you can write something to make HTTP requests to the router to simulate the webUI being used.
I'm guessing most consumer routers are probably pretty simple to talk to. Authentication is probably nothing more than basic realm.
Selenium offers a firefox plugin that lets you record manual interactions with your browser. And then you can export the steps to python, ruby, java or c#. It worked for me to programmatically adjust my router settings to turn off wifi. Clicking on the elements while recording identifies everything you need.
This code works on an Actiontec MI424WR (FIOS)
Edit the code to add your username, password, and router address.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class Routr(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://routerip_or_address"
self.verificationErrors = []
self.accept_next_alert = True
def test_routr(self):
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_name("user_name").clear()
driver.find_element_by_name("user_name").send_keys("your_username")
driver.find_element_by_id("pass2").clear()
driver.find_element_by_id("pass2").send_keys("enter_your_password_here")
driver.find_element_by_link_text("OK").click()
driver.find_element_by_link_text("Change Wireless Settings").click()
driver.find_element_by_id("ws_off").click()
driver.find_element_by_link_text("Apply").click()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException, e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
If this is just a few things you want to change programmatically, simulating HTTP requests should be simple enough. Another option would be to install DD-WRT in your router, basically transforming it into a small Linux installation that allows full programmatic configuration through SSH using standard Linux commands.
I'm unaware of any consumer-grade routers that have an API like that, but you could always build something that (ab)uses the Web interface to do what you want, using something like selenium-rc or watir
MiktoTik sells customer grade routers that allow ssh configuration (mind that they use ssh, but not bash inside ssh). You can even roll your own PHP REST API for router (not that I like PHP, but people are doing it).
I'm not familiar with this router, but I have done similar stuff programmatically via a telnet connection the router with Python.
There's a cood telnet lib for C#:
http://www.codeproject.com/KB/IP/MinimalisticTelnet.aspx
There is a python based Github repo here that describes a SOAP based API. I've used it to program a device schedule for my kids devices. Not willing to pay Disney for Circle. Works great. There's also a js version here.

Resources