Coder Social home page Coder Social logo

icy-bot's Introduction

IcyBot

This is just a small training project and it's not intended for everyday use. Please feel free to give me some tips. This is my first "big" C++ project.

Examples

EXAMPLES ARE NOT UP TO DATE!

Hello World Command

void helloWorld(Bot* bot, Update update) {
	int messageID = update.message.messageID;
	int chatID = update.message.chat.chatID;

	bot->sendChatAction(chatID, TYPING).fire();

	bot->sendMessage(chatID, "*Hello World*")
		.add(Arguments::parseMode, Arguments::markdown)
		.add(Arguments::replyToMessage, messageID)
		.fire();
}

int main() {
	// Initialize your bot
	Bot bot = Bot("TOKEN");
	
	// Add a function in response to /helloWorld
	bot.addCommandHandler("helloWorld", &helloWorld);
	
	// Start getting updates!
	bot.startPolling();
	
	bot.idle();
}

Echo Bot

void updateHandler(Bot* bot, Update update) {
	bot->sendChatAction(update.message.chat.chatID, TYPING).fire();
	bot->sendMessage(update.message.chat.chatID, update.message.text).fire();
}

int main() {
	Bot bot = Bot("TOKEN");
	
	// Add the updates handler, this handler will receive every update
	bot->setUpdateHandler(&updateHandler);
	
	bot.startPolling();
	bot.idle();
	
	return 0;
}

Multiple Bots

// Sample function
void start(Bot* bot, Update update) {
	bot->sendChatAction(update.message.chat.chatID, TYPING).fire();
	bot->sendMessage(update.message.chat.chatID, "Start!").fire();
}

int main() {
	// Initialize your bots
	Bot bot1 = Bot("TOKEN1");
	Bot bot2 = Bot("TOKEN2");
	
	// Add your commands-functions
	bot1.addCommandHandler("start", &start);
	bot2.addCommandHandler("start", &start);
	
	// Start getting updates with both bots!
	bot1.startPolling();
	bot2.startPolling();

	bot1.idle();
	
	return 0;
}

Handle CppTelegramBots

void errorHandler(Bot* bot, Update update, std::string func, std::string error) {
	Log::Error("Handled error: '" + error + "' in command '" + func + "'");
	Log::Debug("Last error has been caused by:\n'" + update.update_json.dump(2) + "'");
}

// Sample function
void generateError(Bot* bot, Update update) {
	bot->sendChatAction(update.message.chat.chatID, TYPING).fire();
	bot->sendMessage(update.message.chat.chatID, "Generating error...").fire();
	
	// throw a test error
	throw std::overflow_error("Divide by zero exception");
}

int main() {
	Bot bot = Bot("TOKEN");
	bot.addCommandHandler("generateError", &generateError);
	
	// Add our error handler
	bot.addErrorHandler(&errorHandler);
	
	bot.startPolling();
	bot.idle();
	
	return 0;
}

Dependencies

cpprestsdk

JSON for Modern C++

Notice:

I'm new to C++

icy-bot's People

Contributors

kaikyutest avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.